blob: dabfa80cf5f2236a6d815e48c8b8b16827379153 [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>
23
24#include <asm/blackfin.h>
Bryan Wu74d362e2008-04-22 22:16:48 +020025#include <asm/portmux.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020026#include <asm/irq.h>
27
Bryan Wud24ecfc2007-05-01 23:26:32 +020028/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020029#define TWI_I2C_MODE_STANDARD 1
30#define TWI_I2C_MODE_STANDARDSUB 2
31#define TWI_I2C_MODE_COMBINED 3
32#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020033
34struct bfin_twi_iface {
Bryan Wud24ecfc2007-05-01 23:26:32 +020035 int irq;
36 spinlock_t lock;
37 char read_write;
38 u8 command;
39 u8 *transPtr;
40 int readNum;
41 int writeNum;
42 int cur_mode;
43 int manual_stop;
44 int result;
Bryan Wud24ecfc2007-05-01 23:26:32 +020045 struct i2c_adapter adap;
46 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020047 struct i2c_msg *pmsg;
48 int msg_num;
49 int cur_msg;
Michael Hennerich958585f2008-07-27 14:41:54 +080050 u16 saved_clkdiv;
51 u16 saved_control;
Bryan Wuaa3d0202008-04-22 22:16:48 +020052 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020053};
54
Bryan Wuaa3d0202008-04-22 22:16:48 +020055
56#define DEFINE_TWI_REG(reg, off) \
57static inline u16 read_##reg(struct bfin_twi_iface *iface) \
58 { return bfin_read16(iface->regs_base + (off)); } \
59static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
60 { bfin_write16(iface->regs_base + (off), v); }
61
62DEFINE_TWI_REG(CLKDIV, 0x00)
63DEFINE_TWI_REG(CONTROL, 0x04)
64DEFINE_TWI_REG(SLAVE_CTL, 0x08)
65DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
66DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
67DEFINE_TWI_REG(MASTER_CTL, 0x14)
68DEFINE_TWI_REG(MASTER_STAT, 0x18)
69DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
70DEFINE_TWI_REG(INT_STAT, 0x20)
71DEFINE_TWI_REG(INT_MASK, 0x24)
72DEFINE_TWI_REG(FIFO_CTL, 0x28)
73DEFINE_TWI_REG(FIFO_STAT, 0x2C)
74DEFINE_TWI_REG(XMT_DATA8, 0x80)
75DEFINE_TWI_REG(XMT_DATA16, 0x84)
76DEFINE_TWI_REG(RCV_DATA8, 0x88)
77DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020078
Bryan Wu74d362e2008-04-22 22:16:48 +020079static const u16 pin_req[2][3] = {
80 {P_TWI0_SCL, P_TWI0_SDA, 0},
81 {P_TWI1_SCL, P_TWI1_SDA, 0},
82};
83
Sonic Zhang5481d072010-03-22 03:23:18 -040084static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
85 unsigned short twi_int_status)
Bryan Wud24ecfc2007-05-01 23:26:32 +020086{
Bryan Wuaa3d0202008-04-22 22:16:48 +020087 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020088
89 if (twi_int_status & XMTSERV) {
90 /* Transmit next data */
91 if (iface->writeNum > 0) {
Sonic Zhang5481d072010-03-22 03:23:18 -040092 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +020093 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020094 iface->writeNum--;
95 }
96 /* start receive immediately after complete sending in
97 * combine mode.
98 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020099 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200100 write_MASTER_CTL(iface,
101 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200102 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200103 write_MASTER_CTL(iface,
104 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200105 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400106 iface->cur_msg + 1 < iface->msg_num) {
107 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
108 write_MASTER_CTL(iface,
109 read_MASTER_CTL(iface) | RSTART | MDIR);
110 else
111 write_MASTER_CTL(iface,
112 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
113 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200114 }
115 if (twi_int_status & RCVSERV) {
116 if (iface->readNum > 0) {
117 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200118 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200119 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
120 /* Change combine mode into sub mode after
121 * read first data.
122 */
123 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
124 /* Get read number from first byte in block
125 * combine mode.
126 */
127 if (iface->readNum == 1 && iface->manual_stop)
128 iface->readNum = *iface->transPtr + 1;
129 }
130 iface->transPtr++;
131 iface->readNum--;
132 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200133 write_MASTER_CTL(iface,
134 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200135 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400136 iface->cur_msg + 1 < iface->msg_num) {
137 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
138 write_MASTER_CTL(iface,
139 read_MASTER_CTL(iface) | RSTART | MDIR);
140 else
141 write_MASTER_CTL(iface,
142 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200143 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200144 }
145 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200146 write_INT_MASK(iface, 0);
147 write_MASTER_STAT(iface, 0x3e);
148 write_MASTER_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200149 iface->result = -EIO;
Michael Hennerich5cfafc12010-03-22 03:23:17 -0400150
151 if (mast_stat & LOSTARB)
152 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
153 if (mast_stat & ANAK)
154 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
155 if (mast_stat & DNAK)
156 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
157 if (mast_stat & BUFRDERR)
158 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
159 if (mast_stat & BUFWRERR)
160 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
161
Bryan Wud24ecfc2007-05-01 23:26:32 +0200162 /* if both err and complete int stats are set, return proper
163 * results.
164 */
165 if (twi_int_status & MCOMP) {
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400166 /* If it is a quick transfer, only address without data,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200167 * not an err, return 1.
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400168 * If address is acknowledged return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200169 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400170 if ((iface->writeNum == 0 && (mast_stat & BUFRDERR))
171 || !(mast_stat & ANAK))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200172 iface->result = 1;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200173 }
174 complete(&iface->complete);
175 return;
176 }
177 if (twi_int_status & MCOMP) {
Bryan Wud24ecfc2007-05-01 23:26:32 +0200178 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
179 if (iface->readNum == 0) {
180 /* set the read number to 1 and ask for manual
181 * stop in block combine mode
182 */
183 iface->readNum = 1;
184 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200185 write_MASTER_CTL(iface,
186 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200187 } else {
188 /* set the readd number in other
189 * combine mode.
190 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200191 write_MASTER_CTL(iface,
192 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200193 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200194 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200195 }
196 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200197 write_MASTER_CTL(iface,
198 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200199 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
200 iface->cur_msg+1 < iface->msg_num) {
201 iface->cur_msg++;
202 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
203 iface->writeNum = iface->readNum =
204 iface->pmsg[iface->cur_msg].len;
205 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200206 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200207 iface->pmsg[iface->cur_msg].addr);
208 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
209 iface->read_write = I2C_SMBUS_READ;
210 else {
211 iface->read_write = I2C_SMBUS_WRITE;
212 /* Transmit first data */
213 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200214 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200215 *(iface->transPtr++));
216 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200217 }
218 }
219
220 if (iface->pmsg[iface->cur_msg].len <= 255)
Sonic Zhang57a8f322009-05-19 07:21:58 -0400221 write_MASTER_CTL(iface,
222 (read_MASTER_CTL(iface) &
223 (~(0xff << 6))) |
224 (iface->pmsg[iface->cur_msg].len << 6));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200225 else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400226 write_MASTER_CTL(iface,
227 (read_MASTER_CTL(iface) |
228 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200229 iface->manual_stop = 1;
230 }
231 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200232 write_MASTER_CTL(iface,
233 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200234 } else {
235 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200236 write_INT_MASK(iface, 0);
237 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200238 }
239 }
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400240 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200241}
242
243/* Interrupt handler */
244static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
245{
246 struct bfin_twi_iface *iface = dev_id;
247 unsigned long flags;
Sonic Zhang5481d072010-03-22 03:23:18 -0400248 unsigned short twi_int_status;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200249
250 spin_lock_irqsave(&iface->lock, flags);
Sonic Zhang5481d072010-03-22 03:23:18 -0400251 while (1) {
252 twi_int_status = read_INT_STAT(iface);
253 if (!twi_int_status)
254 break;
255 /* Clear interrupt status */
256 write_INT_STAT(iface, twi_int_status);
257 bfin_twi_handle_interrupt(iface, twi_int_status);
258 SSYNC();
259 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200260 spin_unlock_irqrestore(&iface->lock, flags);
261 return IRQ_HANDLED;
262}
263
Bryan Wud24ecfc2007-05-01 23:26:32 +0200264/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400265 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200266 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400267static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200268 struct i2c_msg *msgs, int num)
269{
270 struct bfin_twi_iface *iface = adap->algo_data;
271 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200272 int rc = 0;
273
Bryan Wuaa3d0202008-04-22 22:16:48 +0200274 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200275 return -ENXIO;
276
Bryan Wuaa3d0202008-04-22 22:16:48 +0200277 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200278 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200279
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200280 iface->pmsg = msgs;
281 iface->msg_num = num;
282 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200283
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200284 pmsg = &msgs[0];
285 if (pmsg->flags & I2C_M_TEN) {
286 dev_err(&adap->dev, "10 bits addr not supported!\n");
287 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200288 }
289
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200290 iface->cur_mode = TWI_I2C_MODE_REPEAT;
291 iface->manual_stop = 0;
292 iface->transPtr = pmsg->buf;
293 iface->writeNum = iface->readNum = pmsg->len;
294 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200295 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200296 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200297 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200298
299 /* FIFO Initiation. Data in FIFO should be
300 * discarded before start a new operation.
301 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200302 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200303 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200304 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200305 SSYNC();
306
307 if (pmsg->flags & I2C_M_RD)
308 iface->read_write = I2C_SMBUS_READ;
309 else {
310 iface->read_write = I2C_SMBUS_WRITE;
311 /* Transmit first data */
312 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200313 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200314 iface->writeNum--;
315 SSYNC();
316 }
317 }
318
319 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200320 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200321
322 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200323 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200324 SSYNC();
325
326 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200327 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200328 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200329 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200330 iface->manual_stop = 1;
331 }
332
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200333 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200334 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200335 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
336 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
337 SSYNC();
338
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400339 while (!iface->result) {
340 if (!wait_for_completion_timeout(&iface->complete,
341 adap->timeout)) {
342 iface->result = -1;
343 dev_err(&adap->dev, "master transfer timeout\n");
344 }
345 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200346
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400347 if (iface->result == 1)
348 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200349 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400350 rc = iface->result;
351
352 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200353}
354
355/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400356 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200357 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400358static int bfin_twi_master_xfer(struct i2c_adapter *adap,
359 struct i2c_msg *msgs, int num)
360{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400361 return bfin_twi_do_master_xfer(adap, msgs, num);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400362}
363
364/*
365 * One I2C SMBus transfer
366 */
367int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200368 unsigned short flags, char read_write,
369 u8 command, int size, union i2c_smbus_data *data)
370{
371 struct bfin_twi_iface *iface = adap->algo_data;
372 int rc = 0;
373
Bryan Wuaa3d0202008-04-22 22:16:48 +0200374 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200375 return -ENXIO;
376
Bryan Wuaa3d0202008-04-22 22:16:48 +0200377 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200378 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200379
380 iface->writeNum = 0;
381 iface->readNum = 0;
382
383 /* Prepare datas & select mode */
384 switch (size) {
385 case I2C_SMBUS_QUICK:
386 iface->transPtr = NULL;
387 iface->cur_mode = TWI_I2C_MODE_STANDARD;
388 break;
389 case I2C_SMBUS_BYTE:
390 if (data == NULL)
391 iface->transPtr = NULL;
392 else {
393 if (read_write == I2C_SMBUS_READ)
394 iface->readNum = 1;
395 else
396 iface->writeNum = 1;
397 iface->transPtr = &data->byte;
398 }
399 iface->cur_mode = TWI_I2C_MODE_STANDARD;
400 break;
401 case I2C_SMBUS_BYTE_DATA:
402 if (read_write == I2C_SMBUS_READ) {
403 iface->readNum = 1;
404 iface->cur_mode = TWI_I2C_MODE_COMBINED;
405 } else {
406 iface->writeNum = 1;
407 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
408 }
409 iface->transPtr = &data->byte;
410 break;
411 case I2C_SMBUS_WORD_DATA:
412 if (read_write == I2C_SMBUS_READ) {
413 iface->readNum = 2;
414 iface->cur_mode = TWI_I2C_MODE_COMBINED;
415 } else {
416 iface->writeNum = 2;
417 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
418 }
419 iface->transPtr = (u8 *)&data->word;
420 break;
421 case I2C_SMBUS_PROC_CALL:
422 iface->writeNum = 2;
423 iface->readNum = 2;
424 iface->cur_mode = TWI_I2C_MODE_COMBINED;
425 iface->transPtr = (u8 *)&data->word;
426 break;
427 case I2C_SMBUS_BLOCK_DATA:
428 if (read_write == I2C_SMBUS_READ) {
429 iface->readNum = 0;
430 iface->cur_mode = TWI_I2C_MODE_COMBINED;
431 } else {
432 iface->writeNum = data->block[0] + 1;
433 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
434 }
435 iface->transPtr = data->block;
436 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000437 case I2C_SMBUS_I2C_BLOCK_DATA:
438 if (read_write == I2C_SMBUS_READ) {
439 iface->readNum = data->block[0];
440 iface->cur_mode = TWI_I2C_MODE_COMBINED;
441 } else {
442 iface->writeNum = data->block[0];
443 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
444 }
445 iface->transPtr = (u8 *)&data->block[1];
446 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200447 default:
448 return -1;
449 }
450
451 iface->result = 0;
452 iface->manual_stop = 0;
453 iface->read_write = read_write;
454 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200455 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200456
457 /* FIFO Initiation. Data in FIFO should be discarded before
458 * start a new operation.
459 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200460 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200461 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200462 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200463
464 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200465 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200466
467 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200468 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200469 SSYNC();
470
Bryan Wud24ecfc2007-05-01 23:26:32 +0200471 switch (iface->cur_mode) {
472 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200473 write_XMT_DATA8(iface, iface->command);
474 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200475 ((iface->read_write == I2C_SMBUS_READ) ?
476 RCVSERV : XMTSERV));
477 SSYNC();
478
479 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200480 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200481 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200482 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200483 iface->manual_stop = 1;
484 }
485 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200486 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200487 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
488 break;
489 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200490 write_XMT_DATA8(iface, iface->command);
491 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200492 SSYNC();
493
494 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200495 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200496 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200498 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200499 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200500 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
501 break;
502 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200503 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200504 if (size != I2C_SMBUS_QUICK) {
505 /* Don't access xmit data register when this is a
506 * read operation.
507 */
508 if (iface->read_write != I2C_SMBUS_READ) {
509 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200510 write_XMT_DATA8(iface,
511 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200512 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200513 write_MASTER_CTL(iface,
514 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200515 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200516 write_MASTER_CTL(iface,
517 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200518 iface->manual_stop = 1;
519 }
520 iface->writeNum--;
521 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200522 write_XMT_DATA8(iface, iface->command);
523 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200524 }
525 } else {
526 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200527 write_MASTER_CTL(iface,
528 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200529 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200530 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200531 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400532 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200533 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200534 }
535 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200536 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200537 ((iface->read_write == I2C_SMBUS_READ) ?
538 RCVSERV : XMTSERV));
539 SSYNC();
540
541 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200542 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200543 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
544 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
545 break;
546 }
547 SSYNC();
548
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400549 while (!iface->result) {
550 if (!wait_for_completion_timeout(&iface->complete,
551 adap->timeout)) {
552 iface->result = -1;
553 dev_err(&adap->dev, "smbus transfer timeout\n");
554 }
555 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200556
557 rc = (iface->result >= 0) ? 0 : -1;
558
Bryan Wud24ecfc2007-05-01 23:26:32 +0200559 return rc;
560}
561
562/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400563 * Generic I2C SMBus transfer entrypoint
564 */
565int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
566 unsigned short flags, char read_write,
567 u8 command, int size, union i2c_smbus_data *data)
568{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400569 return bfin_twi_do_smbus_xfer(adap, addr, flags,
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400570 read_write, command, size, data);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400571}
572
573/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200574 * Return what the adapter supports
575 */
576static u32 bfin_twi_functionality(struct i2c_adapter *adap)
577{
578 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
579 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
580 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000581 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200582}
583
Bryan Wud24ecfc2007-05-01 23:26:32 +0200584static struct i2c_algorithm bfin_twi_algorithm = {
585 .master_xfer = bfin_twi_master_xfer,
586 .smbus_xfer = bfin_twi_smbus_xfer,
587 .functionality = bfin_twi_functionality,
588};
589
Michael Hennerich958585f2008-07-27 14:41:54 +0800590static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200591{
Michael Hennerich958585f2008-07-27 14:41:54 +0800592 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
593
594 iface->saved_clkdiv = read_CLKDIV(iface);
595 iface->saved_control = read_CONTROL(iface);
596
597 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200598
599 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800600 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200601
602 return 0;
603}
604
Michael Hennerich958585f2008-07-27 14:41:54 +0800605static int i2c_bfin_twi_resume(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200606{
Michael Hennerich958585f2008-07-27 14:41:54 +0800607 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200608
Michael Hennerich958585f2008-07-27 14:41:54 +0800609 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
610 IRQF_DISABLED, pdev->name, iface);
611 if (rc) {
612 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
613 return -ENODEV;
614 }
615
616 /* Resume TWI interface clock as specified */
617 write_CLKDIV(iface, iface->saved_clkdiv);
618
619 /* Resume TWI */
620 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200621
622 return 0;
623}
624
Bryan Wuaa3d0202008-04-22 22:16:48 +0200625static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200626{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200627 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200628 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200629 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200630 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400631 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200632
Bryan Wuaa3d0202008-04-22 22:16:48 +0200633 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
634 if (!iface) {
635 dev_err(&pdev->dev, "Cannot allocate memory\n");
636 rc = -ENOMEM;
637 goto out_error_nomem;
638 }
639
Bryan Wud24ecfc2007-05-01 23:26:32 +0200640 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200641
642 /* Find and map our resources */
643 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
644 if (res == NULL) {
645 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
646 rc = -ENOENT;
647 goto out_error_get_res;
648 }
649
Linus Walleijc6ffdde2009-06-14 00:20:36 +0200650 iface->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200651 if (iface->regs_base == NULL) {
652 dev_err(&pdev->dev, "Cannot map IO\n");
653 rc = -ENXIO;
654 goto out_error_ioremap;
655 }
656
657 iface->irq = platform_get_irq(pdev, 0);
658 if (iface->irq < 0) {
659 dev_err(&pdev->dev, "No IRQ specified\n");
660 rc = -ENOENT;
661 goto out_error_no_irq;
662 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200663
Bryan Wud24ecfc2007-05-01 23:26:32 +0200664 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200665 p_adap->nr = pdev->id;
666 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200667 p_adap->algo = &bfin_twi_algorithm;
668 p_adap->algo_data = iface;
Jean Delvaree1995f62009-01-07 14:29:16 +0100669 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200670 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400671 p_adap->timeout = 5 * HZ;
672 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200673
Bryan Wu74d362e2008-04-22 22:16:48 +0200674 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
675 if (rc) {
676 dev_err(&pdev->dev, "Can't setup pin mux!\n");
677 goto out_error_pin_mux;
678 }
679
Bryan Wud24ecfc2007-05-01 23:26:32 +0200680 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200681 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200682 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200683 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
684 rc = -ENODEV;
685 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200686 }
687
688 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500689 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200690
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400691 /*
692 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500693 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400694 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500695 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400696
Bryan Wud24ecfc2007-05-01 23:26:32 +0200697 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400698 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200699
700 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200701 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200702 SSYNC();
703
Kalle Pokki991dee52008-01-27 18:14:52 +0100704 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200705 if (rc < 0) {
706 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
707 goto out_error_add_adapter;
708 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200709
Bryan Wuaa3d0202008-04-22 22:16:48 +0200710 platform_set_drvdata(pdev, iface);
711
Bryan Wufa6ad222008-04-22 22:16:48 +0200712 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
713 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200714
715 return 0;
716
717out_error_add_adapter:
718 free_irq(iface->irq, iface);
719out_error_req_irq:
720out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200721 peripheral_free_list(pin_req[pdev->id]);
722out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200723 iounmap(iface->regs_base);
724out_error_ioremap:
725out_error_get_res:
726 kfree(iface);
727out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200728 return rc;
729}
730
731static int i2c_bfin_twi_remove(struct platform_device *pdev)
732{
733 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
734
735 platform_set_drvdata(pdev, NULL);
736
737 i2c_del_adapter(&(iface->adap));
738 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200739 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200740 iounmap(iface->regs_base);
741 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200742
743 return 0;
744}
745
746static struct platform_driver i2c_bfin_twi_driver = {
747 .probe = i2c_bfin_twi_probe,
748 .remove = i2c_bfin_twi_remove,
749 .suspend = i2c_bfin_twi_suspend,
750 .resume = i2c_bfin_twi_resume,
751 .driver = {
752 .name = "i2c-bfin-twi",
753 .owner = THIS_MODULE,
754 },
755};
756
757static int __init i2c_bfin_twi_init(void)
758{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200759 return platform_driver_register(&i2c_bfin_twi_driver);
760}
761
762static void __exit i2c_bfin_twi_exit(void)
763{
764 platform_driver_unregister(&i2c_bfin_twi_driver);
765}
766
Bryan Wud24ecfc2007-05-01 23:26:32 +0200767module_init(i2c_bfin_twi_init);
768module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200769
770MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
771MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
772MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200773MODULE_ALIAS("platform:i2c-bfin-twi");