blob: 441134a1df92bf125564c44a760cc628cb54d826 [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
Bryan Wud24ecfc2007-05-01 23:26:32 +020084static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
85{
Bryan Wuaa3d0202008-04-22 22:16:48 +020086 unsigned short twi_int_status = read_INT_STAT(iface);
87 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) {
Bryan Wuaa3d0202008-04-22 22:16:48 +020092 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020093 iface->writeNum--;
94 }
95 /* start receive immediately after complete sending in
96 * combine mode.
97 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020098 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +020099 write_MASTER_CTL(iface,
100 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200101 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200102 write_MASTER_CTL(iface,
103 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200104 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400105 iface->cur_msg + 1 < iface->msg_num) {
106 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
107 write_MASTER_CTL(iface,
108 read_MASTER_CTL(iface) | RSTART | MDIR);
109 else
110 write_MASTER_CTL(iface,
111 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
112 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200113 SSYNC();
114 /* Clear status */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200115 write_INT_STAT(iface, XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200116 SSYNC();
117 }
118 if (twi_int_status & RCVSERV) {
119 if (iface->readNum > 0) {
120 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200121 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200122 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
123 /* Change combine mode into sub mode after
124 * read first data.
125 */
126 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
127 /* Get read number from first byte in block
128 * combine mode.
129 */
130 if (iface->readNum == 1 && iface->manual_stop)
131 iface->readNum = *iface->transPtr + 1;
132 }
133 iface->transPtr++;
134 iface->readNum--;
135 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200136 write_MASTER_CTL(iface,
137 read_MASTER_CTL(iface) | STOP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200138 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200139 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400140 iface->cur_msg + 1 < iface->msg_num) {
141 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
142 write_MASTER_CTL(iface,
143 read_MASTER_CTL(iface) | RSTART | MDIR);
144 else
145 write_MASTER_CTL(iface,
146 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200147 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200148 }
149 /* Clear interrupt source */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200150 write_INT_STAT(iface, RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200151 SSYNC();
152 }
153 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200154 write_INT_STAT(iface, MERR);
155 write_INT_MASK(iface, 0);
156 write_MASTER_STAT(iface, 0x3e);
157 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200158 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200159 iface->result = -EIO;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200160 /* if both err and complete int stats are set, return proper
161 * results.
162 */
163 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200164 write_INT_STAT(iface, MCOMP);
165 write_INT_MASK(iface, 0);
166 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200167 SSYNC();
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400168 /* If it is a quick transfer, only address without data,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200169 * not an err, return 1.
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400170 * If address is acknowledged return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200171 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400172 if ((iface->writeNum == 0 && (mast_stat & BUFRDERR))
173 || !(mast_stat & ANAK))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200174 iface->result = 1;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200175 }
176 complete(&iface->complete);
177 return;
178 }
179 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200180 write_INT_STAT(iface, MCOMP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200181 SSYNC();
182 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
183 if (iface->readNum == 0) {
184 /* set the read number to 1 and ask for manual
185 * stop in block combine mode
186 */
187 iface->readNum = 1;
188 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200189 write_MASTER_CTL(iface,
190 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200191 } else {
192 /* set the readd number in other
193 * combine mode.
194 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200195 write_MASTER_CTL(iface,
196 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200197 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200198 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200199 }
200 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200201 write_MASTER_CTL(iface,
202 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200203 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200204 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
205 iface->cur_msg+1 < iface->msg_num) {
206 iface->cur_msg++;
207 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
208 iface->writeNum = iface->readNum =
209 iface->pmsg[iface->cur_msg].len;
210 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200211 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200212 iface->pmsg[iface->cur_msg].addr);
213 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
214 iface->read_write = I2C_SMBUS_READ;
215 else {
216 iface->read_write = I2C_SMBUS_WRITE;
217 /* Transmit first data */
218 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200219 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200220 *(iface->transPtr++));
221 iface->writeNum--;
222 SSYNC();
223 }
224 }
225
226 if (iface->pmsg[iface->cur_msg].len <= 255)
Sonic Zhang57a8f322009-05-19 07:21:58 -0400227 write_MASTER_CTL(iface,
228 (read_MASTER_CTL(iface) &
229 (~(0xff << 6))) |
230 (iface->pmsg[iface->cur_msg].len << 6));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200231 else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400232 write_MASTER_CTL(iface,
233 (read_MASTER_CTL(iface) |
234 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200235 iface->manual_stop = 1;
236 }
237 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200238 write_MASTER_CTL(iface,
239 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200240 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200241 } else {
242 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200243 write_INT_MASK(iface, 0);
244 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200245 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200246 }
247 }
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400248 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200249}
250
251/* Interrupt handler */
252static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
253{
254 struct bfin_twi_iface *iface = dev_id;
255 unsigned long flags;
256
257 spin_lock_irqsave(&iface->lock, flags);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200258 bfin_twi_handle_interrupt(iface);
259 spin_unlock_irqrestore(&iface->lock, flags);
260 return IRQ_HANDLED;
261}
262
Bryan Wud24ecfc2007-05-01 23:26:32 +0200263/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400264 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200265 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400266static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200267 struct i2c_msg *msgs, int num)
268{
269 struct bfin_twi_iface *iface = adap->algo_data;
270 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200271 int rc = 0;
272
Bryan Wuaa3d0202008-04-22 22:16:48 +0200273 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200274 return -ENXIO;
275
Bryan Wuaa3d0202008-04-22 22:16:48 +0200276 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200277 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200278
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200279 iface->pmsg = msgs;
280 iface->msg_num = num;
281 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200282
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200283 pmsg = &msgs[0];
284 if (pmsg->flags & I2C_M_TEN) {
285 dev_err(&adap->dev, "10 bits addr not supported!\n");
286 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200287 }
288
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200289 iface->cur_mode = TWI_I2C_MODE_REPEAT;
290 iface->manual_stop = 0;
291 iface->transPtr = pmsg->buf;
292 iface->writeNum = iface->readNum = pmsg->len;
293 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200294 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200295 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200296 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200297
298 /* FIFO Initiation. Data in FIFO should be
299 * discarded before start a new operation.
300 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200301 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200302 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200303 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200304 SSYNC();
305
306 if (pmsg->flags & I2C_M_RD)
307 iface->read_write = I2C_SMBUS_READ;
308 else {
309 iface->read_write = I2C_SMBUS_WRITE;
310 /* Transmit first data */
311 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200312 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200313 iface->writeNum--;
314 SSYNC();
315 }
316 }
317
318 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200319 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200320
321 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200322 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200323 SSYNC();
324
325 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200326 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200327 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200328 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200329 iface->manual_stop = 1;
330 }
331
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200332 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200333 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200334 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
335 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
336 SSYNC();
337
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400338 while (!iface->result) {
339 if (!wait_for_completion_timeout(&iface->complete,
340 adap->timeout)) {
341 iface->result = -1;
342 dev_err(&adap->dev, "master transfer timeout\n");
343 }
344 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200345
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400346 if (iface->result == 1)
347 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200348 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400349 rc = iface->result;
350
351 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200352}
353
354/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400355 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200356 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400357static int bfin_twi_master_xfer(struct i2c_adapter *adap,
358 struct i2c_msg *msgs, int num)
359{
360 int i, ret = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200361
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400362 for (i = 0; i < adap->retries; i++) {
363 ret = bfin_twi_do_master_xfer(adap, msgs, num);
364 if (ret > 0)
365 break;
366 }
367
368 return ret;
369}
370
371/*
372 * One I2C SMBus transfer
373 */
374int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200375 unsigned short flags, char read_write,
376 u8 command, int size, union i2c_smbus_data *data)
377{
378 struct bfin_twi_iface *iface = adap->algo_data;
379 int rc = 0;
380
Bryan Wuaa3d0202008-04-22 22:16:48 +0200381 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200382 return -ENXIO;
383
Bryan Wuaa3d0202008-04-22 22:16:48 +0200384 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200385 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200386
387 iface->writeNum = 0;
388 iface->readNum = 0;
389
390 /* Prepare datas & select mode */
391 switch (size) {
392 case I2C_SMBUS_QUICK:
393 iface->transPtr = NULL;
394 iface->cur_mode = TWI_I2C_MODE_STANDARD;
395 break;
396 case I2C_SMBUS_BYTE:
397 if (data == NULL)
398 iface->transPtr = NULL;
399 else {
400 if (read_write == I2C_SMBUS_READ)
401 iface->readNum = 1;
402 else
403 iface->writeNum = 1;
404 iface->transPtr = &data->byte;
405 }
406 iface->cur_mode = TWI_I2C_MODE_STANDARD;
407 break;
408 case I2C_SMBUS_BYTE_DATA:
409 if (read_write == I2C_SMBUS_READ) {
410 iface->readNum = 1;
411 iface->cur_mode = TWI_I2C_MODE_COMBINED;
412 } else {
413 iface->writeNum = 1;
414 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
415 }
416 iface->transPtr = &data->byte;
417 break;
418 case I2C_SMBUS_WORD_DATA:
419 if (read_write == I2C_SMBUS_READ) {
420 iface->readNum = 2;
421 iface->cur_mode = TWI_I2C_MODE_COMBINED;
422 } else {
423 iface->writeNum = 2;
424 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
425 }
426 iface->transPtr = (u8 *)&data->word;
427 break;
428 case I2C_SMBUS_PROC_CALL:
429 iface->writeNum = 2;
430 iface->readNum = 2;
431 iface->cur_mode = TWI_I2C_MODE_COMBINED;
432 iface->transPtr = (u8 *)&data->word;
433 break;
434 case I2C_SMBUS_BLOCK_DATA:
435 if (read_write == I2C_SMBUS_READ) {
436 iface->readNum = 0;
437 iface->cur_mode = TWI_I2C_MODE_COMBINED;
438 } else {
439 iface->writeNum = data->block[0] + 1;
440 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
441 }
442 iface->transPtr = data->block;
443 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000444 case I2C_SMBUS_I2C_BLOCK_DATA:
445 if (read_write == I2C_SMBUS_READ) {
446 iface->readNum = data->block[0];
447 iface->cur_mode = TWI_I2C_MODE_COMBINED;
448 } else {
449 iface->writeNum = data->block[0];
450 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
451 }
452 iface->transPtr = (u8 *)&data->block[1];
453 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200454 default:
455 return -1;
456 }
457
458 iface->result = 0;
459 iface->manual_stop = 0;
460 iface->read_write = read_write;
461 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200462 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200463
464 /* FIFO Initiation. Data in FIFO should be discarded before
465 * start a new operation.
466 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200467 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200468 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200469 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200470
471 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200472 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200473
474 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200475 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200476 SSYNC();
477
Bryan Wud24ecfc2007-05-01 23:26:32 +0200478 switch (iface->cur_mode) {
479 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200480 write_XMT_DATA8(iface, iface->command);
481 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200482 ((iface->read_write == I2C_SMBUS_READ) ?
483 RCVSERV : XMTSERV));
484 SSYNC();
485
486 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200487 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200488 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200489 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200490 iface->manual_stop = 1;
491 }
492 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200493 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200494 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
495 break;
496 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_XMT_DATA8(iface, iface->command);
498 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200499 SSYNC();
500
501 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200502 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200503 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200504 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200505 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200506 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200507 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
508 break;
509 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200510 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200511 if (size != I2C_SMBUS_QUICK) {
512 /* Don't access xmit data register when this is a
513 * read operation.
514 */
515 if (iface->read_write != I2C_SMBUS_READ) {
516 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200517 write_XMT_DATA8(iface,
518 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200519 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200520 write_MASTER_CTL(iface,
521 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200522 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200523 write_MASTER_CTL(iface,
524 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200525 iface->manual_stop = 1;
526 }
527 iface->writeNum--;
528 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200529 write_XMT_DATA8(iface, iface->command);
530 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200531 }
532 } else {
533 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200534 write_MASTER_CTL(iface,
535 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200536 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200537 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200538 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400539 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200540 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200541 }
542 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200543 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200544 ((iface->read_write == I2C_SMBUS_READ) ?
545 RCVSERV : XMTSERV));
546 SSYNC();
547
548 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200549 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200550 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
551 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
552 break;
553 }
554 SSYNC();
555
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400556 while (!iface->result) {
557 if (!wait_for_completion_timeout(&iface->complete,
558 adap->timeout)) {
559 iface->result = -1;
560 dev_err(&adap->dev, "smbus transfer timeout\n");
561 }
562 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200563
564 rc = (iface->result >= 0) ? 0 : -1;
565
Bryan Wud24ecfc2007-05-01 23:26:32 +0200566 return rc;
567}
568
569/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400570 * Generic I2C SMBus transfer entrypoint
571 */
572int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
573 unsigned short flags, char read_write,
574 u8 command, int size, union i2c_smbus_data *data)
575{
576 int i, ret = 0;
577
578 for (i = 0; i < adap->retries; i++) {
579 ret = bfin_twi_do_smbus_xfer(adap, addr, flags,
580 read_write, command, size, data);
581 if (ret == 0)
582 break;
583 }
584
585 return ret;
586}
587
588/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200589 * Return what the adapter supports
590 */
591static u32 bfin_twi_functionality(struct i2c_adapter *adap)
592{
593 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
594 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
595 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000596 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200597}
598
Bryan Wud24ecfc2007-05-01 23:26:32 +0200599static struct i2c_algorithm bfin_twi_algorithm = {
600 .master_xfer = bfin_twi_master_xfer,
601 .smbus_xfer = bfin_twi_smbus_xfer,
602 .functionality = bfin_twi_functionality,
603};
604
Michael Hennerich958585f2008-07-27 14:41:54 +0800605static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
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);
608
609 iface->saved_clkdiv = read_CLKDIV(iface);
610 iface->saved_control = read_CONTROL(iface);
611
612 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200613
614 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800615 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200616
617 return 0;
618}
619
Michael Hennerich958585f2008-07-27 14:41:54 +0800620static int i2c_bfin_twi_resume(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200621{
Michael Hennerich958585f2008-07-27 14:41:54 +0800622 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200623
Michael Hennerich958585f2008-07-27 14:41:54 +0800624 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
625 IRQF_DISABLED, pdev->name, iface);
626 if (rc) {
627 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
628 return -ENODEV;
629 }
630
631 /* Resume TWI interface clock as specified */
632 write_CLKDIV(iface, iface->saved_clkdiv);
633
634 /* Resume TWI */
635 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200636
637 return 0;
638}
639
Bryan Wuaa3d0202008-04-22 22:16:48 +0200640static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200641{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200642 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200643 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200644 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200645 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400646 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200647
Bryan Wuaa3d0202008-04-22 22:16:48 +0200648 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
649 if (!iface) {
650 dev_err(&pdev->dev, "Cannot allocate memory\n");
651 rc = -ENOMEM;
652 goto out_error_nomem;
653 }
654
Bryan Wud24ecfc2007-05-01 23:26:32 +0200655 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200656
657 /* Find and map our resources */
658 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
659 if (res == NULL) {
660 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
661 rc = -ENOENT;
662 goto out_error_get_res;
663 }
664
Linus Walleijc6ffdde2009-06-14 00:20:36 +0200665 iface->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200666 if (iface->regs_base == NULL) {
667 dev_err(&pdev->dev, "Cannot map IO\n");
668 rc = -ENXIO;
669 goto out_error_ioremap;
670 }
671
672 iface->irq = platform_get_irq(pdev, 0);
673 if (iface->irq < 0) {
674 dev_err(&pdev->dev, "No IRQ specified\n");
675 rc = -ENOENT;
676 goto out_error_no_irq;
677 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200678
Bryan Wud24ecfc2007-05-01 23:26:32 +0200679 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200680 p_adap->nr = pdev->id;
681 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200682 p_adap->algo = &bfin_twi_algorithm;
683 p_adap->algo_data = iface;
Jean Delvaree1995f62009-01-07 14:29:16 +0100684 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200685 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400686 p_adap->timeout = 5 * HZ;
687 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200688
Bryan Wu74d362e2008-04-22 22:16:48 +0200689 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
690 if (rc) {
691 dev_err(&pdev->dev, "Can't setup pin mux!\n");
692 goto out_error_pin_mux;
693 }
694
Bryan Wud24ecfc2007-05-01 23:26:32 +0200695 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200696 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200697 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200698 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
699 rc = -ENODEV;
700 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200701 }
702
703 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500704 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200705
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400706 /*
707 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500708 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400709 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500710 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400711
Bryan Wud24ecfc2007-05-01 23:26:32 +0200712 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400713 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200714
715 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200716 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200717 SSYNC();
718
Kalle Pokki991dee52008-01-27 18:14:52 +0100719 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200720 if (rc < 0) {
721 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
722 goto out_error_add_adapter;
723 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200724
Bryan Wuaa3d0202008-04-22 22:16:48 +0200725 platform_set_drvdata(pdev, iface);
726
Bryan Wufa6ad222008-04-22 22:16:48 +0200727 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
728 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200729
730 return 0;
731
732out_error_add_adapter:
733 free_irq(iface->irq, iface);
734out_error_req_irq:
735out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200736 peripheral_free_list(pin_req[pdev->id]);
737out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200738 iounmap(iface->regs_base);
739out_error_ioremap:
740out_error_get_res:
741 kfree(iface);
742out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200743 return rc;
744}
745
746static int i2c_bfin_twi_remove(struct platform_device *pdev)
747{
748 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
749
750 platform_set_drvdata(pdev, NULL);
751
752 i2c_del_adapter(&(iface->adap));
753 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200754 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200755 iounmap(iface->regs_base);
756 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200757
758 return 0;
759}
760
761static struct platform_driver i2c_bfin_twi_driver = {
762 .probe = i2c_bfin_twi_probe,
763 .remove = i2c_bfin_twi_remove,
764 .suspend = i2c_bfin_twi_suspend,
765 .resume = i2c_bfin_twi_resume,
766 .driver = {
767 .name = "i2c-bfin-twi",
768 .owner = THIS_MODULE,
769 },
770};
771
772static int __init i2c_bfin_twi_init(void)
773{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200774 return platform_driver_register(&i2c_bfin_twi_driver);
775}
776
777static void __exit i2c_bfin_twi_exit(void)
778{
779 platform_driver_unregister(&i2c_bfin_twi_driver);
780}
781
Bryan Wud24ecfc2007-05-01 23:26:32 +0200782module_init(i2c_bfin_twi_init);
783module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200784
785MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
786MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
787MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200788MODULE_ALIAS("platform:i2c-bfin-twi");