blob: af162b4c7a6d9b8b30756c53147eb3de458b3d5e [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>
Sonic Zhang45126da2014-01-28 16:55:21 +080024#include <linux/i2c/bfin_twi.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020025
Bryan Wud24ecfc2007-05-01 23:26:32 +020026#include <asm/irq.h>
Sonic Zhang45126da2014-01-28 16:55:21 +080027#include <asm/portmux.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) {
Bryan Wuaa3d0202008-04-22 22:16:48 +020068 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020069 iface->writeNum--;
70 }
Bryan Wud24ecfc2007-05-01 23:26:32 +020071 }
72 if (twi_int_status & RCVSERV) {
Sonic Zhang8419c8d2013-05-28 18:41:09 +080073 while (iface->readNum > 0 &&
74 (read_FIFO_STAT(iface) & RCVSTAT)) {
Bryan Wud24ecfc2007-05-01 23:26:32 +020075 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +020076 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020077 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
78 /* Change combine mode into sub mode after
79 * read first data.
80 */
81 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
82 /* Get read number from first byte in block
83 * combine mode.
84 */
85 if (iface->readNum == 1 && iface->manual_stop)
86 iface->readNum = *iface->transPtr + 1;
87 }
88 iface->transPtr++;
89 iface->readNum--;
Sonic Zhanga20a64d2012-06-13 16:22:41 +080090 }
91
92 if (iface->readNum == 0) {
93 if (iface->manual_stop) {
94 /* Temporary workaround to avoid possible bus stall -
95 * Flush FIFO before issuing the STOP condition
96 */
97 read_RCV_DATA16(iface);
Frank Shew94327d02009-05-19 07:23:49 -040098 write_MASTER_CTL(iface,
Sonic Zhanga20a64d2012-06-13 16:22:41 +080099 read_MASTER_CTL(iface) | STOP);
100 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
101 iface->cur_msg + 1 < iface->msg_num) {
102 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
103 write_MASTER_CTL(iface,
Sonic Zhang28a377c2012-06-13 16:22:44 +0800104 read_MASTER_CTL(iface) | MDIR);
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800105 else
106 write_MASTER_CTL(iface,
Sonic Zhang28a377c2012-06-13 16:22:44 +0800107 read_MASTER_CTL(iface) & ~MDIR);
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800108 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200109 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200110 }
111 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200112 write_INT_MASK(iface, 0);
113 write_MASTER_STAT(iface, 0x3e);
114 write_MASTER_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200115 iface->result = -EIO;
Michael Hennerich5cfafc12010-03-22 03:23:17 -0400116
117 if (mast_stat & LOSTARB)
118 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
119 if (mast_stat & ANAK)
120 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
121 if (mast_stat & DNAK)
122 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
123 if (mast_stat & BUFRDERR)
124 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
125 if (mast_stat & BUFWRERR)
126 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
127
Michael Hennerich540ac552011-01-11 00:25:08 -0500128 /* Faulty slave devices, may drive SDA low after a transfer
129 * finishes. To release the bus this code generates up to 9
130 * extra clocks until SDA is released.
131 */
132
133 if (read_MASTER_STAT(iface) & SDASEN) {
134 int cnt = 9;
135 do {
136 write_MASTER_CTL(iface, SCLOVR);
137 udelay(6);
138 write_MASTER_CTL(iface, 0);
139 udelay(6);
140 } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);
141
142 write_MASTER_CTL(iface, SDAOVR | SCLOVR);
143 udelay(6);
144 write_MASTER_CTL(iface, SDAOVR);
145 udelay(6);
146 write_MASTER_CTL(iface, 0);
147 }
148
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400149 /* If it is a quick transfer, only address without data,
150 * not an err, return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200151 */
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400152 if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
153 iface->transPtr == NULL &&
154 (twi_int_status & MCOMP) && (mast_stat & DNAK))
155 iface->result = 1;
156
Bryan Wud24ecfc2007-05-01 23:26:32 +0200157 complete(&iface->complete);
158 return;
159 }
160 if (twi_int_status & MCOMP) {
Sonic Zhang2ee74eb2012-06-13 16:22:43 +0800161 if (twi_int_status & (XMTSERV | RCVSERV) &&
162 (read_MASTER_CTL(iface) & MEN) == 0 &&
Sonic Zhang4a651632011-06-23 17:07:54 -0400163 (iface->cur_mode == TWI_I2C_MODE_REPEAT ||
164 iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
165 iface->result = -1;
166 write_INT_MASK(iface, 0);
167 write_MASTER_CTL(iface, 0);
168 } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
Bryan Wud24ecfc2007-05-01 23:26:32 +0200169 if (iface->readNum == 0) {
170 /* set the read number to 1 and ask for manual
171 * stop in block combine mode
172 */
173 iface->readNum = 1;
174 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200175 write_MASTER_CTL(iface,
176 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200177 } else {
178 /* set the readd number in other
179 * combine mode.
180 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200181 write_MASTER_CTL(iface,
182 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200183 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200184 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200185 }
186 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200187 write_MASTER_CTL(iface,
188 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200189 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Sonic Zhang28a377c2012-06-13 16:22:44 +0800190 iface->cur_msg + 1 < iface->msg_num) {
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200191 iface->cur_msg++;
192 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
193 iface->writeNum = iface->readNum =
194 iface->pmsg[iface->cur_msg].len;
195 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200196 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200197 iface->pmsg[iface->cur_msg].addr);
198 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
199 iface->read_write = I2C_SMBUS_READ;
200 else {
201 iface->read_write = I2C_SMBUS_WRITE;
202 /* Transmit first data */
203 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200204 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200205 *(iface->transPtr++));
206 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200207 }
208 }
209
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800210 if (iface->pmsg[iface->cur_msg].len <= 255) {
211 write_MASTER_CTL(iface,
Sonic Zhang57a8f322009-05-19 07:21:58 -0400212 (read_MASTER_CTL(iface) &
213 (~(0xff << 6))) |
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800214 (iface->pmsg[iface->cur_msg].len << 6));
215 iface->manual_stop = 0;
216 } else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400217 write_MASTER_CTL(iface,
218 (read_MASTER_CTL(iface) |
219 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200220 iface->manual_stop = 1;
221 }
Sonic Zhang28a377c2012-06-13 16:22:44 +0800222 /* remove restart bit before last message */
223 if (iface->cur_msg + 1 == iface->msg_num)
224 write_MASTER_CTL(iface,
225 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200226 } else {
227 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200228 write_INT_MASK(iface, 0);
229 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200230 }
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800231 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200232 }
233}
234
235/* Interrupt handler */
236static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
237{
238 struct bfin_twi_iface *iface = dev_id;
239 unsigned long flags;
Sonic Zhang5481d072010-03-22 03:23:18 -0400240 unsigned short twi_int_status;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200241
242 spin_lock_irqsave(&iface->lock, flags);
Sonic Zhang5481d072010-03-22 03:23:18 -0400243 while (1) {
244 twi_int_status = read_INT_STAT(iface);
245 if (!twi_int_status)
246 break;
247 /* Clear interrupt status */
248 write_INT_STAT(iface, twi_int_status);
249 bfin_twi_handle_interrupt(iface, twi_int_status);
Sonic Zhang5481d072010-03-22 03:23:18 -0400250 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200251 spin_unlock_irqrestore(&iface->lock, flags);
252 return IRQ_HANDLED;
253}
254
Bryan Wud24ecfc2007-05-01 23:26:32 +0200255/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400256 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200257 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400258static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200259 struct i2c_msg *msgs, int num)
260{
261 struct bfin_twi_iface *iface = adap->algo_data;
262 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200263 int rc = 0;
264
Bryan Wuaa3d0202008-04-22 22:16:48 +0200265 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200266 return -ENXIO;
267
Sonic Zhanga25733d2012-06-13 16:22:42 +0800268 if (read_MASTER_STAT(iface) & BUSBUSY)
269 return -EAGAIN;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200270
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200271 iface->pmsg = msgs;
272 iface->msg_num = num;
273 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200274
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200275 pmsg = &msgs[0];
276 if (pmsg->flags & I2C_M_TEN) {
277 dev_err(&adap->dev, "10 bits addr not supported!\n");
278 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200279 }
280
Sonic Zhang28a377c2012-06-13 16:22:44 +0800281 if (iface->msg_num > 1)
282 iface->cur_mode = TWI_I2C_MODE_REPEAT;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200283 iface->manual_stop = 0;
284 iface->transPtr = pmsg->buf;
285 iface->writeNum = iface->readNum = pmsg->len;
286 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200287 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200288 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200289 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200290
291 /* FIFO Initiation. Data in FIFO should be
292 * discarded before start a new operation.
293 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200294 write_FIFO_CTL(iface, 0x3);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200295 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200296
297 if (pmsg->flags & I2C_M_RD)
298 iface->read_write = I2C_SMBUS_READ;
299 else {
300 iface->read_write = I2C_SMBUS_WRITE;
301 /* Transmit first data */
302 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200303 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200304 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200305 }
306 }
307
308 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200309 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200310
311 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200312 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200313
314 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200315 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200316 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200317 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200318 iface->manual_stop = 1;
319 }
320
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200321 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200322 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang28a377c2012-06-13 16:22:44 +0800323 (iface->msg_num > 1 ? RSTART : 0) |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200324 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
325 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200326
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400327 while (!iface->result) {
328 if (!wait_for_completion_timeout(&iface->complete,
329 adap->timeout)) {
330 iface->result = -1;
331 dev_err(&adap->dev, "master transfer timeout\n");
332 }
333 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200334
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400335 if (iface->result == 1)
336 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200337 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400338 rc = iface->result;
339
340 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200341}
342
343/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400344 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200345 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400346static int bfin_twi_master_xfer(struct i2c_adapter *adap,
347 struct i2c_msg *msgs, int num)
348{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400349 return bfin_twi_do_master_xfer(adap, msgs, num);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400350}
351
352/*
353 * One I2C SMBus transfer
354 */
355int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200356 unsigned short flags, char read_write,
357 u8 command, int size, union i2c_smbus_data *data)
358{
359 struct bfin_twi_iface *iface = adap->algo_data;
360 int rc = 0;
361
Bryan Wuaa3d0202008-04-22 22:16:48 +0200362 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200363 return -ENXIO;
364
Sonic Zhanga25733d2012-06-13 16:22:42 +0800365 if (read_MASTER_STAT(iface) & BUSBUSY)
366 return -EAGAIN;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200367
368 iface->writeNum = 0;
369 iface->readNum = 0;
370
371 /* Prepare datas & select mode */
372 switch (size) {
373 case I2C_SMBUS_QUICK:
374 iface->transPtr = NULL;
375 iface->cur_mode = TWI_I2C_MODE_STANDARD;
376 break;
377 case I2C_SMBUS_BYTE:
378 if (data == NULL)
379 iface->transPtr = NULL;
380 else {
381 if (read_write == I2C_SMBUS_READ)
382 iface->readNum = 1;
383 else
384 iface->writeNum = 1;
385 iface->transPtr = &data->byte;
386 }
387 iface->cur_mode = TWI_I2C_MODE_STANDARD;
388 break;
389 case I2C_SMBUS_BYTE_DATA:
390 if (read_write == I2C_SMBUS_READ) {
391 iface->readNum = 1;
392 iface->cur_mode = TWI_I2C_MODE_COMBINED;
393 } else {
394 iface->writeNum = 1;
395 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
396 }
397 iface->transPtr = &data->byte;
398 break;
399 case I2C_SMBUS_WORD_DATA:
400 if (read_write == I2C_SMBUS_READ) {
401 iface->readNum = 2;
402 iface->cur_mode = TWI_I2C_MODE_COMBINED;
403 } else {
404 iface->writeNum = 2;
405 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
406 }
407 iface->transPtr = (u8 *)&data->word;
408 break;
409 case I2C_SMBUS_PROC_CALL:
410 iface->writeNum = 2;
411 iface->readNum = 2;
412 iface->cur_mode = TWI_I2C_MODE_COMBINED;
413 iface->transPtr = (u8 *)&data->word;
414 break;
415 case I2C_SMBUS_BLOCK_DATA:
416 if (read_write == I2C_SMBUS_READ) {
417 iface->readNum = 0;
418 iface->cur_mode = TWI_I2C_MODE_COMBINED;
419 } else {
420 iface->writeNum = data->block[0] + 1;
421 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
422 }
423 iface->transPtr = data->block;
424 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000425 case I2C_SMBUS_I2C_BLOCK_DATA:
426 if (read_write == I2C_SMBUS_READ) {
427 iface->readNum = data->block[0];
428 iface->cur_mode = TWI_I2C_MODE_COMBINED;
429 } else {
430 iface->writeNum = data->block[0];
431 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
432 }
433 iface->transPtr = (u8 *)&data->block[1];
434 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200435 default:
436 return -1;
437 }
438
439 iface->result = 0;
440 iface->manual_stop = 0;
441 iface->read_write = read_write;
442 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200443 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200444
445 /* FIFO Initiation. Data in FIFO should be discarded before
446 * start a new operation.
447 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200448 write_FIFO_CTL(iface, 0x3);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200449 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200450
451 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200452 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200453
454 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200455 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200456
Bryan Wud24ecfc2007-05-01 23:26:32 +0200457 switch (iface->cur_mode) {
458 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200459 write_XMT_DATA8(iface, iface->command);
460 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200461 ((iface->read_write == I2C_SMBUS_READ) ?
462 RCVSERV : XMTSERV));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200463
464 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200465 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200466 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200467 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200468 iface->manual_stop = 1;
469 }
470 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200471 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200472 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
473 break;
474 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200475 write_XMT_DATA8(iface, iface->command);
476 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200477
478 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200479 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200480 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200481 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200482 /* Master enable */
Sonic Zhang28a377c2012-06-13 16:22:44 +0800483 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | RSTART |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200484 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
485 break;
486 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200487 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200488 if (size != I2C_SMBUS_QUICK) {
489 /* Don't access xmit data register when this is a
490 * read operation.
491 */
492 if (iface->read_write != I2C_SMBUS_READ) {
493 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200494 write_XMT_DATA8(iface,
495 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200496 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_MASTER_CTL(iface,
498 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200499 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200500 write_MASTER_CTL(iface,
501 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200502 iface->manual_stop = 1;
503 }
504 iface->writeNum--;
505 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200506 write_XMT_DATA8(iface, iface->command);
507 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200508 }
509 } else {
510 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200511 write_MASTER_CTL(iface,
512 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200513 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200514 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200515 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400516 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200517 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200518 }
519 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200520 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200521 ((iface->read_write == I2C_SMBUS_READ) ?
522 RCVSERV : XMTSERV));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200523
524 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200525 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200526 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
527 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
528 break;
529 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200530
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400531 while (!iface->result) {
532 if (!wait_for_completion_timeout(&iface->complete,
533 adap->timeout)) {
534 iface->result = -1;
535 dev_err(&adap->dev, "smbus transfer timeout\n");
536 }
537 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200538
539 rc = (iface->result >= 0) ? 0 : -1;
540
Bryan Wud24ecfc2007-05-01 23:26:32 +0200541 return rc;
542}
543
544/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400545 * Generic I2C SMBus transfer entrypoint
546 */
547int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
548 unsigned short flags, char read_write,
549 u8 command, int size, union i2c_smbus_data *data)
550{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400551 return bfin_twi_do_smbus_xfer(adap, addr, flags,
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400552 read_write, command, size, data);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400553}
554
555/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200556 * Return what the adapter supports
557 */
558static u32 bfin_twi_functionality(struct i2c_adapter *adap)
559{
560 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
561 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
562 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000563 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200564}
565
Bryan Wud24ecfc2007-05-01 23:26:32 +0200566static struct i2c_algorithm bfin_twi_algorithm = {
567 .master_xfer = bfin_twi_master_xfer,
568 .smbus_xfer = bfin_twi_smbus_xfer,
569 .functionality = bfin_twi_functionality,
570};
571
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900572#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200573static int i2c_bfin_twi_suspend(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200574{
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200575 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Michael Hennerich958585f2008-07-27 14:41:54 +0800576
577 iface->saved_clkdiv = read_CLKDIV(iface);
578 iface->saved_control = read_CONTROL(iface);
579
580 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200581
582 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800583 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200584
585 return 0;
586}
587
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200588static int i2c_bfin_twi_resume(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200589{
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200590 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200591
Michael Hennerich958585f2008-07-27 14:41:54 +0800592 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200593 0, to_platform_device(dev)->name, iface);
Michael Hennerich958585f2008-07-27 14:41:54 +0800594 if (rc) {
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200595 dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
Michael Hennerich958585f2008-07-27 14:41:54 +0800596 return -ENODEV;
597 }
598
599 /* Resume TWI interface clock as specified */
600 write_CLKDIV(iface, iface->saved_clkdiv);
601
602 /* Resume TWI */
603 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200604
605 return 0;
606}
607
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200608static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
609 i2c_bfin_twi_suspend, i2c_bfin_twi_resume);
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900610#define I2C_BFIN_TWI_PM_OPS (&i2c_bfin_twi_pm)
611#else
612#define I2C_BFIN_TWI_PM_OPS NULL
613#endif
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200614
Bryan Wuaa3d0202008-04-22 22:16:48 +0200615static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200616{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200617 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200618 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200619 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200620 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400621 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200622
Sonic Zhang0709dc92014-06-03 13:02:06 +0800623 iface = devm_kzalloc(&pdev->dev, sizeof(struct bfin_twi_iface),
624 GFP_KERNEL);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200625 if (!iface) {
626 dev_err(&pdev->dev, "Cannot allocate memory\n");
Sonic Zhang0709dc92014-06-03 13:02:06 +0800627 return -ENOMEM;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200628 }
629
Bryan Wud24ecfc2007-05-01 23:26:32 +0200630 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200631
632 /* Find and map our resources */
633 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sonic Zhang0709dc92014-06-03 13:02:06 +0800634 iface->regs_base = devm_ioremap_resource(&pdev->dev, res);
635 if (IS_ERR(iface->regs_base)) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200636 dev_err(&pdev->dev, "Cannot map IO\n");
Sonic Zhang0709dc92014-06-03 13:02:06 +0800637 return PTR_ERR(iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200638 }
639
640 iface->irq = platform_get_irq(pdev, 0);
641 if (iface->irq < 0) {
642 dev_err(&pdev->dev, "No IRQ specified\n");
Sonic Zhang0709dc92014-06-03 13:02:06 +0800643 return -ENOENT;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200644 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200645
Bryan Wud24ecfc2007-05-01 23:26:32 +0200646 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200647 p_adap->nr = pdev->id;
648 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200649 p_adap->algo = &bfin_twi_algorithm;
650 p_adap->algo_data = iface;
Wolfram Sangaa5b7752014-07-10 13:46:24 +0200651 p_adap->class = I2C_CLASS_DEPRECATED;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200652 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400653 p_adap->timeout = 5 * HZ;
654 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200655
Jingoo Han6d4028c2013-07-30 16:59:33 +0900656 rc = peripheral_request_list(
Jingoo Han3c41aa72013-09-09 14:32:25 +0900657 dev_get_platdata(&pdev->dev),
Jingoo Han6d4028c2013-07-30 16:59:33 +0900658 "i2c-bfin-twi");
Bryan Wu74d362e2008-04-22 22:16:48 +0200659 if (rc) {
660 dev_err(&pdev->dev, "Can't setup pin mux!\n");
Sonic Zhang0709dc92014-06-03 13:02:06 +0800661 return -EBUSY;
Bryan Wu74d362e2008-04-22 22:16:48 +0200662 }
663
Sonic Zhang0709dc92014-06-03 13:02:06 +0800664 rc = devm_request_irq(&pdev->dev, iface->irq, bfin_twi_interrupt_entry,
Yong Zhang43110512011-09-21 17:28:33 +0800665 0, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200666 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200667 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
668 rc = -ENODEV;
Sonic Zhang0709dc92014-06-03 13:02:06 +0800669 goto out_error;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200670 }
671
672 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500673 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200674
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400675 /*
676 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500677 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400678 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500679 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400680
Bryan Wud24ecfc2007-05-01 23:26:32 +0200681 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400682 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200683
684 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200685 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200686
Kalle Pokki991dee52008-01-27 18:14:52 +0100687 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200688 if (rc < 0) {
689 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
Sonic Zhang0709dc92014-06-03 13:02:06 +0800690 goto out_error;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200691 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200692
Bryan Wuaa3d0202008-04-22 22:16:48 +0200693 platform_set_drvdata(pdev, iface);
694
Bryan Wufa6ad222008-04-22 22:16:48 +0200695 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
696 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200697
698 return 0;
699
Sonic Zhang0709dc92014-06-03 13:02:06 +0800700out_error:
Jingoo Han3c41aa72013-09-09 14:32:25 +0900701 peripheral_free_list(dev_get_platdata(&pdev->dev));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200702 return rc;
703}
704
705static int i2c_bfin_twi_remove(struct platform_device *pdev)
706{
707 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
708
Bryan Wud24ecfc2007-05-01 23:26:32 +0200709 i2c_del_adapter(&(iface->adap));
Jingoo Han3c41aa72013-09-09 14:32:25 +0900710 peripheral_free_list(dev_get_platdata(&pdev->dev));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200711
712 return 0;
713}
714
715static struct platform_driver i2c_bfin_twi_driver = {
716 .probe = i2c_bfin_twi_probe,
717 .remove = i2c_bfin_twi_remove,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200718 .driver = {
719 .name = "i2c-bfin-twi",
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900720 .pm = I2C_BFIN_TWI_PM_OPS,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200721 },
722};
723
724static int __init i2c_bfin_twi_init(void)
725{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200726 return platform_driver_register(&i2c_bfin_twi_driver);
727}
728
729static void __exit i2c_bfin_twi_exit(void)
730{
731 platform_driver_unregister(&i2c_bfin_twi_driver);
732}
733
Michael Hennerich74f56c42011-01-11 00:25:09 -0500734subsys_initcall(i2c_bfin_twi_init);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200735module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200736
737MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
738MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
739MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200740MODULE_ALIAS("platform:i2c-bfin-twi");