blob: 52b545a795f2ae6e533bf0920385623e8e6e1b04 [file] [log] [blame]
Bryan Wud24ecfc2007-05-01 23:26:32 +02001/*
Mike Frysingerbd584992008-04-22 22:16:48 +02002 * Blackfin On-Chip Two Wire Interface Driver
Bryan Wud24ecfc2007-05-01 23:26:32 +02003 *
Mike Frysingerbd584992008-04-22 22:16:48 +02004 * Copyright 2005-2007 Analog Devices Inc.
Bryan Wud24ecfc2007-05-01 23:26:32 +02005 *
Mike Frysingerbd584992008-04-22 22:16:48 +02006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wud24ecfc2007-05-01 23:26:32 +02007 *
Mike Frysingerbd584992008-04-22 22:16:48 +02008 * Licensed under the GPL-2 or later.
Bryan Wud24ecfc2007-05-01 23:26:32 +02009 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mike Frysinger6df263c2009-06-14 01:55:37 -040016#include <linux/io.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020017#include <linux/mm.h>
18#include <linux/timer.h>
19#include <linux/spinlock.h>
20#include <linux/completion.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
Michael Hennerich540ac552011-01-11 00:25:08 -050023#include <linux/delay.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020024
25#include <asm/blackfin.h>
Bryan Wu74d362e2008-04-22 22:16:48 +020026#include <asm/portmux.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020027#include <asm/irq.h>
28
Bryan Wud24ecfc2007-05-01 23:26:32 +020029/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020030#define TWI_I2C_MODE_STANDARD 1
31#define TWI_I2C_MODE_STANDARDSUB 2
32#define TWI_I2C_MODE_COMBINED 3
33#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020034
35struct bfin_twi_iface {
Bryan Wud24ecfc2007-05-01 23:26:32 +020036 int irq;
37 spinlock_t lock;
38 char read_write;
39 u8 command;
40 u8 *transPtr;
41 int readNum;
42 int writeNum;
43 int cur_mode;
44 int manual_stop;
45 int result;
Bryan Wud24ecfc2007-05-01 23:26:32 +020046 struct i2c_adapter adap;
47 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020048 struct i2c_msg *pmsg;
49 int msg_num;
50 int cur_msg;
Michael Hennerich958585f2008-07-27 14:41:54 +080051 u16 saved_clkdiv;
52 u16 saved_control;
Bryan Wuaa3d0202008-04-22 22:16:48 +020053 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020054};
55
Bryan Wuaa3d0202008-04-22 22:16:48 +020056
57#define DEFINE_TWI_REG(reg, off) \
58static inline u16 read_##reg(struct bfin_twi_iface *iface) \
59 { return bfin_read16(iface->regs_base + (off)); } \
60static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
61 { bfin_write16(iface->regs_base + (off), v); }
62
63DEFINE_TWI_REG(CLKDIV, 0x00)
64DEFINE_TWI_REG(CONTROL, 0x04)
65DEFINE_TWI_REG(SLAVE_CTL, 0x08)
66DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
67DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
68DEFINE_TWI_REG(MASTER_CTL, 0x14)
69DEFINE_TWI_REG(MASTER_STAT, 0x18)
70DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
71DEFINE_TWI_REG(INT_STAT, 0x20)
72DEFINE_TWI_REG(INT_MASK, 0x24)
73DEFINE_TWI_REG(FIFO_CTL, 0x28)
74DEFINE_TWI_REG(FIFO_STAT, 0x2C)
75DEFINE_TWI_REG(XMT_DATA8, 0x80)
76DEFINE_TWI_REG(XMT_DATA16, 0x84)
77DEFINE_TWI_REG(RCV_DATA8, 0x88)
78DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020079
Bryan Wu74d362e2008-04-22 22:16:48 +020080static const u16 pin_req[2][3] = {
81 {P_TWI0_SCL, P_TWI0_SDA, 0},
82 {P_TWI1_SCL, P_TWI1_SDA, 0},
83};
84
Sonic Zhang5481d072010-03-22 03:23:18 -040085static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
86 unsigned short twi_int_status)
Bryan Wud24ecfc2007-05-01 23:26:32 +020087{
Bryan Wuaa3d0202008-04-22 22:16:48 +020088 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020089
90 if (twi_int_status & XMTSERV) {
91 /* Transmit next data */
92 if (iface->writeNum > 0) {
Sonic Zhang5481d072010-03-22 03:23:18 -040093 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +020094 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020095 iface->writeNum--;
96 }
97 /* start receive immediately after complete sending in
98 * combine mode.
99 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200100 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200101 write_MASTER_CTL(iface,
102 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200103 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200104 write_MASTER_CTL(iface,
105 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200106 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400107 iface->cur_msg + 1 < iface->msg_num) {
108 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
109 write_MASTER_CTL(iface,
110 read_MASTER_CTL(iface) | RSTART | MDIR);
111 else
112 write_MASTER_CTL(iface,
113 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
114 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200115 }
116 if (twi_int_status & RCVSERV) {
117 if (iface->readNum > 0) {
118 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200119 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200120 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
121 /* Change combine mode into sub mode after
122 * read first data.
123 */
124 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
125 /* Get read number from first byte in block
126 * combine mode.
127 */
128 if (iface->readNum == 1 && iface->manual_stop)
129 iface->readNum = *iface->transPtr + 1;
130 }
131 iface->transPtr++;
132 iface->readNum--;
133 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200134 write_MASTER_CTL(iface,
135 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200136 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400137 iface->cur_msg + 1 < iface->msg_num) {
138 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
139 write_MASTER_CTL(iface,
140 read_MASTER_CTL(iface) | RSTART | MDIR);
141 else
142 write_MASTER_CTL(iface,
143 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200144 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200145 }
146 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200147 write_INT_MASK(iface, 0);
148 write_MASTER_STAT(iface, 0x3e);
149 write_MASTER_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200150 iface->result = -EIO;
Michael Hennerich5cfafc12010-03-22 03:23:17 -0400151
152 if (mast_stat & LOSTARB)
153 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
154 if (mast_stat & ANAK)
155 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
156 if (mast_stat & DNAK)
157 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
158 if (mast_stat & BUFRDERR)
159 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
160 if (mast_stat & BUFWRERR)
161 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
162
Michael Hennerich540ac552011-01-11 00:25:08 -0500163 /* Faulty slave devices, may drive SDA low after a transfer
164 * finishes. To release the bus this code generates up to 9
165 * extra clocks until SDA is released.
166 */
167
168 if (read_MASTER_STAT(iface) & SDASEN) {
169 int cnt = 9;
170 do {
171 write_MASTER_CTL(iface, SCLOVR);
172 udelay(6);
173 write_MASTER_CTL(iface, 0);
174 udelay(6);
175 } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);
176
177 write_MASTER_CTL(iface, SDAOVR | SCLOVR);
178 udelay(6);
179 write_MASTER_CTL(iface, SDAOVR);
180 udelay(6);
181 write_MASTER_CTL(iface, 0);
182 }
183
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400184 /* If it is a quick transfer, only address without data,
185 * not an err, return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200186 */
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400187 if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
188 iface->transPtr == NULL &&
189 (twi_int_status & MCOMP) && (mast_stat & DNAK))
190 iface->result = 1;
191
Bryan Wud24ecfc2007-05-01 23:26:32 +0200192 complete(&iface->complete);
193 return;
194 }
195 if (twi_int_status & MCOMP) {
Bryan Wud24ecfc2007-05-01 23:26:32 +0200196 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
197 if (iface->readNum == 0) {
198 /* set the read number to 1 and ask for manual
199 * stop in block combine mode
200 */
201 iface->readNum = 1;
202 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200203 write_MASTER_CTL(iface,
204 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200205 } else {
206 /* set the readd number in other
207 * combine mode.
208 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200209 write_MASTER_CTL(iface,
210 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200211 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200212 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200213 }
214 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200215 write_MASTER_CTL(iface,
216 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200217 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
218 iface->cur_msg+1 < iface->msg_num) {
219 iface->cur_msg++;
220 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
221 iface->writeNum = iface->readNum =
222 iface->pmsg[iface->cur_msg].len;
223 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200224 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200225 iface->pmsg[iface->cur_msg].addr);
226 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
227 iface->read_write = I2C_SMBUS_READ;
228 else {
229 iface->read_write = I2C_SMBUS_WRITE;
230 /* Transmit first data */
231 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200232 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200233 *(iface->transPtr++));
234 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200235 }
236 }
237
238 if (iface->pmsg[iface->cur_msg].len <= 255)
Sonic Zhang57a8f322009-05-19 07:21:58 -0400239 write_MASTER_CTL(iface,
240 (read_MASTER_CTL(iface) &
241 (~(0xff << 6))) |
242 (iface->pmsg[iface->cur_msg].len << 6));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200243 else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400244 write_MASTER_CTL(iface,
245 (read_MASTER_CTL(iface) |
246 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200247 iface->manual_stop = 1;
248 }
249 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200250 write_MASTER_CTL(iface,
251 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200252 } else {
253 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200254 write_INT_MASK(iface, 0);
255 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200256 }
257 }
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400258 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200259}
260
261/* Interrupt handler */
262static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
263{
264 struct bfin_twi_iface *iface = dev_id;
265 unsigned long flags;
Sonic Zhang5481d072010-03-22 03:23:18 -0400266 unsigned short twi_int_status;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200267
268 spin_lock_irqsave(&iface->lock, flags);
Sonic Zhang5481d072010-03-22 03:23:18 -0400269 while (1) {
270 twi_int_status = read_INT_STAT(iface);
271 if (!twi_int_status)
272 break;
273 /* Clear interrupt status */
274 write_INT_STAT(iface, twi_int_status);
275 bfin_twi_handle_interrupt(iface, twi_int_status);
276 SSYNC();
277 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200278 spin_unlock_irqrestore(&iface->lock, flags);
279 return IRQ_HANDLED;
280}
281
Bryan Wud24ecfc2007-05-01 23:26:32 +0200282/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400283 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200284 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400285static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200286 struct i2c_msg *msgs, int num)
287{
288 struct bfin_twi_iface *iface = adap->algo_data;
289 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200290 int rc = 0;
291
Bryan Wuaa3d0202008-04-22 22:16:48 +0200292 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200293 return -ENXIO;
294
Bryan Wuaa3d0202008-04-22 22:16:48 +0200295 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200296 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200297
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200298 iface->pmsg = msgs;
299 iface->msg_num = num;
300 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200301
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200302 pmsg = &msgs[0];
303 if (pmsg->flags & I2C_M_TEN) {
304 dev_err(&adap->dev, "10 bits addr not supported!\n");
305 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200306 }
307
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200308 iface->cur_mode = TWI_I2C_MODE_REPEAT;
309 iface->manual_stop = 0;
310 iface->transPtr = pmsg->buf;
311 iface->writeNum = iface->readNum = pmsg->len;
312 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200313 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200314 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200315 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200316
317 /* FIFO Initiation. Data in FIFO should be
318 * discarded before start a new operation.
319 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200320 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200321 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200322 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200323 SSYNC();
324
325 if (pmsg->flags & I2C_M_RD)
326 iface->read_write = I2C_SMBUS_READ;
327 else {
328 iface->read_write = I2C_SMBUS_WRITE;
329 /* Transmit first data */
330 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200331 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200332 iface->writeNum--;
333 SSYNC();
334 }
335 }
336
337 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200338 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200339
340 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200341 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200342 SSYNC();
343
344 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200345 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200346 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200347 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200348 iface->manual_stop = 1;
349 }
350
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200351 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200352 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200353 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
354 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
355 SSYNC();
356
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400357 while (!iface->result) {
358 if (!wait_for_completion_timeout(&iface->complete,
359 adap->timeout)) {
360 iface->result = -1;
361 dev_err(&adap->dev, "master transfer timeout\n");
362 }
363 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200364
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400365 if (iface->result == 1)
366 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200367 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400368 rc = iface->result;
369
370 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200371}
372
373/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400374 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200375 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400376static int bfin_twi_master_xfer(struct i2c_adapter *adap,
377 struct i2c_msg *msgs, int num)
378{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400379 return bfin_twi_do_master_xfer(adap, msgs, num);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400380}
381
382/*
383 * One I2C SMBus transfer
384 */
385int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200386 unsigned short flags, char read_write,
387 u8 command, int size, union i2c_smbus_data *data)
388{
389 struct bfin_twi_iface *iface = adap->algo_data;
390 int rc = 0;
391
Bryan Wuaa3d0202008-04-22 22:16:48 +0200392 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200393 return -ENXIO;
394
Bryan Wuaa3d0202008-04-22 22:16:48 +0200395 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200396 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200397
398 iface->writeNum = 0;
399 iface->readNum = 0;
400
401 /* Prepare datas & select mode */
402 switch (size) {
403 case I2C_SMBUS_QUICK:
404 iface->transPtr = NULL;
405 iface->cur_mode = TWI_I2C_MODE_STANDARD;
406 break;
407 case I2C_SMBUS_BYTE:
408 if (data == NULL)
409 iface->transPtr = NULL;
410 else {
411 if (read_write == I2C_SMBUS_READ)
412 iface->readNum = 1;
413 else
414 iface->writeNum = 1;
415 iface->transPtr = &data->byte;
416 }
417 iface->cur_mode = TWI_I2C_MODE_STANDARD;
418 break;
419 case I2C_SMBUS_BYTE_DATA:
420 if (read_write == I2C_SMBUS_READ) {
421 iface->readNum = 1;
422 iface->cur_mode = TWI_I2C_MODE_COMBINED;
423 } else {
424 iface->writeNum = 1;
425 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
426 }
427 iface->transPtr = &data->byte;
428 break;
429 case I2C_SMBUS_WORD_DATA:
430 if (read_write == I2C_SMBUS_READ) {
431 iface->readNum = 2;
432 iface->cur_mode = TWI_I2C_MODE_COMBINED;
433 } else {
434 iface->writeNum = 2;
435 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
436 }
437 iface->transPtr = (u8 *)&data->word;
438 break;
439 case I2C_SMBUS_PROC_CALL:
440 iface->writeNum = 2;
441 iface->readNum = 2;
442 iface->cur_mode = TWI_I2C_MODE_COMBINED;
443 iface->transPtr = (u8 *)&data->word;
444 break;
445 case I2C_SMBUS_BLOCK_DATA:
446 if (read_write == I2C_SMBUS_READ) {
447 iface->readNum = 0;
448 iface->cur_mode = TWI_I2C_MODE_COMBINED;
449 } else {
450 iface->writeNum = data->block[0] + 1;
451 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
452 }
453 iface->transPtr = data->block;
454 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000455 case I2C_SMBUS_I2C_BLOCK_DATA:
456 if (read_write == I2C_SMBUS_READ) {
457 iface->readNum = data->block[0];
458 iface->cur_mode = TWI_I2C_MODE_COMBINED;
459 } else {
460 iface->writeNum = data->block[0];
461 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
462 }
463 iface->transPtr = (u8 *)&data->block[1];
464 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200465 default:
466 return -1;
467 }
468
469 iface->result = 0;
470 iface->manual_stop = 0;
471 iface->read_write = read_write;
472 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200473 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200474
475 /* FIFO Initiation. Data in FIFO should be discarded before
476 * start a new operation.
477 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200478 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200479 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200480 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200481
482 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200483 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200484
485 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200486 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200487 SSYNC();
488
Bryan Wud24ecfc2007-05-01 23:26:32 +0200489 switch (iface->cur_mode) {
490 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200491 write_XMT_DATA8(iface, iface->command);
492 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200493 ((iface->read_write == I2C_SMBUS_READ) ?
494 RCVSERV : XMTSERV));
495 SSYNC();
496
497 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200498 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200499 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200500 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200501 iface->manual_stop = 1;
502 }
503 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200504 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200505 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
506 break;
507 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200508 write_XMT_DATA8(iface, iface->command);
509 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200510 SSYNC();
511
512 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200513 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200514 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200515 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200516 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200517 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200518 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
519 break;
520 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200521 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200522 if (size != I2C_SMBUS_QUICK) {
523 /* Don't access xmit data register when this is a
524 * read operation.
525 */
526 if (iface->read_write != I2C_SMBUS_READ) {
527 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200528 write_XMT_DATA8(iface,
529 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200530 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200531 write_MASTER_CTL(iface,
532 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200533 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200534 write_MASTER_CTL(iface,
535 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200536 iface->manual_stop = 1;
537 }
538 iface->writeNum--;
539 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200540 write_XMT_DATA8(iface, iface->command);
541 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200542 }
543 } else {
544 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200545 write_MASTER_CTL(iface,
546 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200547 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200548 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200549 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400550 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200551 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200552 }
553 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200554 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200555 ((iface->read_write == I2C_SMBUS_READ) ?
556 RCVSERV : XMTSERV));
557 SSYNC();
558
559 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200560 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200561 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
562 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
563 break;
564 }
565 SSYNC();
566
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400567 while (!iface->result) {
568 if (!wait_for_completion_timeout(&iface->complete,
569 adap->timeout)) {
570 iface->result = -1;
571 dev_err(&adap->dev, "smbus transfer timeout\n");
572 }
573 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200574
575 rc = (iface->result >= 0) ? 0 : -1;
576
Bryan Wud24ecfc2007-05-01 23:26:32 +0200577 return rc;
578}
579
580/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400581 * Generic I2C SMBus transfer entrypoint
582 */
583int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
584 unsigned short flags, char read_write,
585 u8 command, int size, union i2c_smbus_data *data)
586{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400587 return bfin_twi_do_smbus_xfer(adap, addr, flags,
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400588 read_write, command, size, data);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400589}
590
591/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200592 * Return what the adapter supports
593 */
594static u32 bfin_twi_functionality(struct i2c_adapter *adap)
595{
596 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
597 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
598 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000599 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200600}
601
Bryan Wud24ecfc2007-05-01 23:26:32 +0200602static struct i2c_algorithm bfin_twi_algorithm = {
603 .master_xfer = bfin_twi_master_xfer,
604 .smbus_xfer = bfin_twi_smbus_xfer,
605 .functionality = bfin_twi_functionality,
606};
607
Michael Hennerich958585f2008-07-27 14:41:54 +0800608static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200609{
Michael Hennerich958585f2008-07-27 14:41:54 +0800610 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
611
612 iface->saved_clkdiv = read_CLKDIV(iface);
613 iface->saved_control = read_CONTROL(iface);
614
615 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200616
617 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800618 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200619
620 return 0;
621}
622
Michael Hennerich958585f2008-07-27 14:41:54 +0800623static int i2c_bfin_twi_resume(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200624{
Michael Hennerich958585f2008-07-27 14:41:54 +0800625 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200626
Michael Hennerich958585f2008-07-27 14:41:54 +0800627 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
628 IRQF_DISABLED, pdev->name, iface);
629 if (rc) {
630 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
631 return -ENODEV;
632 }
633
634 /* Resume TWI interface clock as specified */
635 write_CLKDIV(iface, iface->saved_clkdiv);
636
637 /* Resume TWI */
638 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200639
640 return 0;
641}
642
Bryan Wuaa3d0202008-04-22 22:16:48 +0200643static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200644{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200645 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200646 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200647 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200648 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400649 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200650
Bryan Wuaa3d0202008-04-22 22:16:48 +0200651 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
652 if (!iface) {
653 dev_err(&pdev->dev, "Cannot allocate memory\n");
654 rc = -ENOMEM;
655 goto out_error_nomem;
656 }
657
Bryan Wud24ecfc2007-05-01 23:26:32 +0200658 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200659
660 /* Find and map our resources */
661 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
662 if (res == NULL) {
663 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
664 rc = -ENOENT;
665 goto out_error_get_res;
666 }
667
Linus Walleijc6ffdde2009-06-14 00:20:36 +0200668 iface->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200669 if (iface->regs_base == NULL) {
670 dev_err(&pdev->dev, "Cannot map IO\n");
671 rc = -ENXIO;
672 goto out_error_ioremap;
673 }
674
675 iface->irq = platform_get_irq(pdev, 0);
676 if (iface->irq < 0) {
677 dev_err(&pdev->dev, "No IRQ specified\n");
678 rc = -ENOENT;
679 goto out_error_no_irq;
680 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200681
Bryan Wud24ecfc2007-05-01 23:26:32 +0200682 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200683 p_adap->nr = pdev->id;
684 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200685 p_adap->algo = &bfin_twi_algorithm;
686 p_adap->algo_data = iface;
Jean Delvaree1995f62009-01-07 14:29:16 +0100687 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200688 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400689 p_adap->timeout = 5 * HZ;
690 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200691
Bryan Wu74d362e2008-04-22 22:16:48 +0200692 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
693 if (rc) {
694 dev_err(&pdev->dev, "Can't setup pin mux!\n");
695 goto out_error_pin_mux;
696 }
697
Bryan Wud24ecfc2007-05-01 23:26:32 +0200698 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200699 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200700 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200701 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
702 rc = -ENODEV;
703 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200704 }
705
706 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500707 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200708
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400709 /*
710 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500711 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400712 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500713 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400714
Bryan Wud24ecfc2007-05-01 23:26:32 +0200715 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400716 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200717
718 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200719 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200720 SSYNC();
721
Kalle Pokki991dee52008-01-27 18:14:52 +0100722 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200723 if (rc < 0) {
724 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
725 goto out_error_add_adapter;
726 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200727
Bryan Wuaa3d0202008-04-22 22:16:48 +0200728 platform_set_drvdata(pdev, iface);
729
Bryan Wufa6ad222008-04-22 22:16:48 +0200730 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
731 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200732
733 return 0;
734
735out_error_add_adapter:
736 free_irq(iface->irq, iface);
737out_error_req_irq:
738out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200739 peripheral_free_list(pin_req[pdev->id]);
740out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200741 iounmap(iface->regs_base);
742out_error_ioremap:
743out_error_get_res:
744 kfree(iface);
745out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200746 return rc;
747}
748
749static int i2c_bfin_twi_remove(struct platform_device *pdev)
750{
751 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
752
753 platform_set_drvdata(pdev, NULL);
754
755 i2c_del_adapter(&(iface->adap));
756 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200757 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200758 iounmap(iface->regs_base);
759 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200760
761 return 0;
762}
763
764static struct platform_driver i2c_bfin_twi_driver = {
765 .probe = i2c_bfin_twi_probe,
766 .remove = i2c_bfin_twi_remove,
767 .suspend = i2c_bfin_twi_suspend,
768 .resume = i2c_bfin_twi_resume,
769 .driver = {
770 .name = "i2c-bfin-twi",
771 .owner = THIS_MODULE,
772 },
773};
774
775static int __init i2c_bfin_twi_init(void)
776{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200777 return platform_driver_register(&i2c_bfin_twi_driver);
778}
779
780static void __exit i2c_bfin_twi_exit(void)
781{
782 platform_driver_unregister(&i2c_bfin_twi_driver);
783}
784
Michael Hennerich74f56c42011-01-11 00:25:09 -0500785subsys_initcall(i2c_bfin_twi_init);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200786module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200787
788MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
789MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
790MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200791MODULE_ALIAS("platform:i2c-bfin-twi");