blob: f2b558f17ca21a78f27f44270c03f4617219ca7b [file] [log] [blame]
Bryan Wud24ecfc2007-05-01 23:26:32 +02001/*
2 * drivers/i2c/busses/i2c-bfin-twi.c
3 *
4 * Description: Driver for Blackfin Two Wire Interface
5 *
6 * Author: sonicz <sonic.zhang@analog.com>
7 *
8 * Copyright (c) 2005-2007 Analog Devices, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/i2c.h>
29#include <linux/mm.h>
30#include <linux/timer.h>
31#include <linux/spinlock.h>
32#include <linux/completion.h>
33#include <linux/interrupt.h>
34#include <linux/platform_device.h>
35
36#include <asm/blackfin.h>
37#include <asm/irq.h>
38
39#define POLL_TIMEOUT (2 * HZ)
40
41/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020042#define TWI_I2C_MODE_STANDARD 1
43#define TWI_I2C_MODE_STANDARDSUB 2
44#define TWI_I2C_MODE_COMBINED 3
45#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020046
47struct bfin_twi_iface {
Bryan Wud24ecfc2007-05-01 23:26:32 +020048 int irq;
49 spinlock_t lock;
50 char read_write;
51 u8 command;
52 u8 *transPtr;
53 int readNum;
54 int writeNum;
55 int cur_mode;
56 int manual_stop;
57 int result;
58 int timeout_count;
59 struct timer_list timeout_timer;
60 struct i2c_adapter adap;
61 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020062 struct i2c_msg *pmsg;
63 int msg_num;
64 int cur_msg;
Bryan Wuaa3d0202008-04-22 22:16:48 +020065 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020066};
67
Bryan Wuaa3d0202008-04-22 22:16:48 +020068
69#define DEFINE_TWI_REG(reg, off) \
70static inline u16 read_##reg(struct bfin_twi_iface *iface) \
71 { return bfin_read16(iface->regs_base + (off)); } \
72static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
73 { bfin_write16(iface->regs_base + (off), v); }
74
75DEFINE_TWI_REG(CLKDIV, 0x00)
76DEFINE_TWI_REG(CONTROL, 0x04)
77DEFINE_TWI_REG(SLAVE_CTL, 0x08)
78DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
79DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
80DEFINE_TWI_REG(MASTER_CTL, 0x14)
81DEFINE_TWI_REG(MASTER_STAT, 0x18)
82DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
83DEFINE_TWI_REG(INT_STAT, 0x20)
84DEFINE_TWI_REG(INT_MASK, 0x24)
85DEFINE_TWI_REG(FIFO_CTL, 0x28)
86DEFINE_TWI_REG(FIFO_STAT, 0x2C)
87DEFINE_TWI_REG(XMT_DATA8, 0x80)
88DEFINE_TWI_REG(XMT_DATA16, 0x84)
89DEFINE_TWI_REG(RCV_DATA8, 0x88)
90DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020091
92static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
93{
Bryan Wuaa3d0202008-04-22 22:16:48 +020094 unsigned short twi_int_status = read_INT_STAT(iface);
95 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020096
97 if (twi_int_status & XMTSERV) {
98 /* Transmit next data */
99 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200100 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200101 iface->writeNum--;
102 }
103 /* start receive immediately after complete sending in
104 * combine mode.
105 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200106 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200107 write_MASTER_CTL(iface,
108 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200109 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200110 write_MASTER_CTL(iface,
111 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200112 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
113 iface->cur_msg+1 < iface->msg_num)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200114 write_MASTER_CTL(iface,
115 read_MASTER_CTL(iface) | RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200116 SSYNC();
117 /* Clear status */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200118 write_INT_STAT(iface, XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200119 SSYNC();
120 }
121 if (twi_int_status & RCVSERV) {
122 if (iface->readNum > 0) {
123 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200124 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200125 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
126 /* Change combine mode into sub mode after
127 * read first data.
128 */
129 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
130 /* Get read number from first byte in block
131 * combine mode.
132 */
133 if (iface->readNum == 1 && iface->manual_stop)
134 iface->readNum = *iface->transPtr + 1;
135 }
136 iface->transPtr++;
137 iface->readNum--;
138 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200139 write_MASTER_CTL(iface,
140 read_MASTER_CTL(iface) | STOP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200141 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200142 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
143 iface->cur_msg+1 < iface->msg_num) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200144 write_MASTER_CTL(iface,
145 read_MASTER_CTL(iface) | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200146 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200147 }
148 /* Clear interrupt source */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200149 write_INT_STAT(iface, RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200150 SSYNC();
151 }
152 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200153 write_INT_STAT(iface, MERR);
154 write_INT_MASK(iface, 0);
155 write_MASTER_STAT(iface, 0x3e);
156 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200157 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200158 iface->result = -EIO;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200159 /* if both err and complete int stats are set, return proper
160 * results.
161 */
162 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200163 write_INT_STAT(iface, MCOMP);
164 write_INT_MASK(iface, 0);
165 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200166 SSYNC();
167 /* If it is a quick transfer, only address bug no data,
168 * not an err, return 1.
169 */
170 if (iface->writeNum == 0 && (mast_stat & BUFRDERR))
171 iface->result = 1;
172 /* If address not acknowledged return -1,
173 * else return 0.
174 */
175 else if (!(mast_stat & ANAK))
176 iface->result = 0;
177 }
178 complete(&iface->complete);
179 return;
180 }
181 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200182 write_INT_STAT(iface, MCOMP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200183 SSYNC();
184 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
185 if (iface->readNum == 0) {
186 /* set the read number to 1 and ask for manual
187 * stop in block combine mode
188 */
189 iface->readNum = 1;
190 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200191 write_MASTER_CTL(iface,
192 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200193 } else {
194 /* set the readd number in other
195 * combine mode.
196 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200197 write_MASTER_CTL(iface,
198 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200199 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200200 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200201 }
202 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200203 write_MASTER_CTL(iface,
204 read_MASTER_CTL(iface) & ~RSTART);
205 write_MASTER_CTL(iface,
206 read_MASTER_CTL(iface) | MEN | MDIR);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200207 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200208 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
209 iface->cur_msg+1 < iface->msg_num) {
210 iface->cur_msg++;
211 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
212 iface->writeNum = iface->readNum =
213 iface->pmsg[iface->cur_msg].len;
214 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200215 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200216 iface->pmsg[iface->cur_msg].addr);
217 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
218 iface->read_write = I2C_SMBUS_READ;
219 else {
220 iface->read_write = I2C_SMBUS_WRITE;
221 /* Transmit first data */
222 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200223 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200224 *(iface->transPtr++));
225 iface->writeNum--;
226 SSYNC();
227 }
228 }
229
230 if (iface->pmsg[iface->cur_msg].len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200231 write_MASTER_CTL(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200232 iface->pmsg[iface->cur_msg].len << 6);
233 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200234 write_MASTER_CTL(iface, 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);
240 write_MASTER_CTL(iface, read_MASTER_CTL(iface) |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200241 MEN | ((iface->read_write == I2C_SMBUS_READ) ?
242 MDIR : 0));
243 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200244 } else {
245 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200246 write_INT_MASK(iface, 0);
247 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200248 SSYNC();
249 complete(&iface->complete);
250 }
251 }
252}
253
254/* Interrupt handler */
255static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
256{
257 struct bfin_twi_iface *iface = dev_id;
258 unsigned long flags;
259
260 spin_lock_irqsave(&iface->lock, flags);
261 del_timer(&iface->timeout_timer);
262 bfin_twi_handle_interrupt(iface);
263 spin_unlock_irqrestore(&iface->lock, flags);
264 return IRQ_HANDLED;
265}
266
267static void bfin_twi_timeout(unsigned long data)
268{
269 struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data;
270 unsigned long flags;
271
272 spin_lock_irqsave(&iface->lock, flags);
273 bfin_twi_handle_interrupt(iface);
274 if (iface->result == 0) {
275 iface->timeout_count--;
276 if (iface->timeout_count > 0) {
277 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
278 add_timer(&iface->timeout_timer);
279 } else {
280 iface->result = -1;
281 complete(&iface->complete);
282 }
283 }
284 spin_unlock_irqrestore(&iface->lock, flags);
285}
286
287/*
288 * Generic i2c master transfer entrypoint
289 */
290static int bfin_twi_master_xfer(struct i2c_adapter *adap,
291 struct i2c_msg *msgs, int num)
292{
293 struct bfin_twi_iface *iface = adap->algo_data;
294 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200295 int rc = 0;
296
Bryan Wuaa3d0202008-04-22 22:16:48 +0200297 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200298 return -ENXIO;
299
Bryan Wuaa3d0202008-04-22 22:16:48 +0200300 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200301 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200302
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200303 iface->pmsg = msgs;
304 iface->msg_num = num;
305 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200306
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200307 pmsg = &msgs[0];
308 if (pmsg->flags & I2C_M_TEN) {
309 dev_err(&adap->dev, "10 bits addr not supported!\n");
310 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200311 }
312
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200313 iface->cur_mode = TWI_I2C_MODE_REPEAT;
314 iface->manual_stop = 0;
315 iface->transPtr = pmsg->buf;
316 iface->writeNum = iface->readNum = pmsg->len;
317 iface->result = 0;
318 iface->timeout_count = 10;
319 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200320 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200321
322 /* FIFO Initiation. Data in FIFO should be
323 * discarded before start a new operation.
324 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200325 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200326 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200327 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200328 SSYNC();
329
330 if (pmsg->flags & I2C_M_RD)
331 iface->read_write = I2C_SMBUS_READ;
332 else {
333 iface->read_write = I2C_SMBUS_WRITE;
334 /* Transmit first data */
335 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200336 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200337 iface->writeNum--;
338 SSYNC();
339 }
340 }
341
342 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200343 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200344
345 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200346 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200347 SSYNC();
348
349 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200350 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200351 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200352 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200353 iface->manual_stop = 1;
354 }
355
356 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
357 add_timer(&iface->timeout_timer);
358
359 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200360 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200361 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
362 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
363 SSYNC();
364
365 wait_for_completion(&iface->complete);
366
367 rc = iface->result;
368
369 if (rc == 1)
370 return num;
371 else
372 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200373}
374
375/*
376 * SMBus type transfer entrypoint
377 */
378
379int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
380 unsigned short flags, char read_write,
381 u8 command, int size, union i2c_smbus_data *data)
382{
383 struct bfin_twi_iface *iface = adap->algo_data;
384 int rc = 0;
385
Bryan Wuaa3d0202008-04-22 22:16:48 +0200386 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200387 return -ENXIO;
388
Bryan Wuaa3d0202008-04-22 22:16:48 +0200389 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200390 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200391
392 iface->writeNum = 0;
393 iface->readNum = 0;
394
395 /* Prepare datas & select mode */
396 switch (size) {
397 case I2C_SMBUS_QUICK:
398 iface->transPtr = NULL;
399 iface->cur_mode = TWI_I2C_MODE_STANDARD;
400 break;
401 case I2C_SMBUS_BYTE:
402 if (data == NULL)
403 iface->transPtr = NULL;
404 else {
405 if (read_write == I2C_SMBUS_READ)
406 iface->readNum = 1;
407 else
408 iface->writeNum = 1;
409 iface->transPtr = &data->byte;
410 }
411 iface->cur_mode = TWI_I2C_MODE_STANDARD;
412 break;
413 case I2C_SMBUS_BYTE_DATA:
414 if (read_write == I2C_SMBUS_READ) {
415 iface->readNum = 1;
416 iface->cur_mode = TWI_I2C_MODE_COMBINED;
417 } else {
418 iface->writeNum = 1;
419 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
420 }
421 iface->transPtr = &data->byte;
422 break;
423 case I2C_SMBUS_WORD_DATA:
424 if (read_write == I2C_SMBUS_READ) {
425 iface->readNum = 2;
426 iface->cur_mode = TWI_I2C_MODE_COMBINED;
427 } else {
428 iface->writeNum = 2;
429 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
430 }
431 iface->transPtr = (u8 *)&data->word;
432 break;
433 case I2C_SMBUS_PROC_CALL:
434 iface->writeNum = 2;
435 iface->readNum = 2;
436 iface->cur_mode = TWI_I2C_MODE_COMBINED;
437 iface->transPtr = (u8 *)&data->word;
438 break;
439 case I2C_SMBUS_BLOCK_DATA:
440 if (read_write == I2C_SMBUS_READ) {
441 iface->readNum = 0;
442 iface->cur_mode = TWI_I2C_MODE_COMBINED;
443 } else {
444 iface->writeNum = data->block[0] + 1;
445 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
446 }
447 iface->transPtr = data->block;
448 break;
449 default:
450 return -1;
451 }
452
453 iface->result = 0;
454 iface->manual_stop = 0;
455 iface->read_write = read_write;
456 iface->command = command;
457 iface->timeout_count = 10;
458
459 /* FIFO Initiation. Data in FIFO should be discarded before
460 * start a new operation.
461 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200462 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200463 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200464 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200465
466 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200467 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200468
469 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200470 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200471 SSYNC();
472
473 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
474 add_timer(&iface->timeout_timer);
475
476 switch (iface->cur_mode) {
477 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200478 write_XMT_DATA8(iface, iface->command);
479 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200480 ((iface->read_write == I2C_SMBUS_READ) ?
481 RCVSERV : XMTSERV));
482 SSYNC();
483
484 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200485 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200486 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200487 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200488 iface->manual_stop = 1;
489 }
490 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200491 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200492 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
493 break;
494 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200495 write_XMT_DATA8(iface, iface->command);
496 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200497 SSYNC();
498
499 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200500 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200501 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200502 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200503 /* 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 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200508 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200509 if (size != I2C_SMBUS_QUICK) {
510 /* Don't access xmit data register when this is a
511 * read operation.
512 */
513 if (iface->read_write != I2C_SMBUS_READ) {
514 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200515 write_XMT_DATA8(iface,
516 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200517 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200518 write_MASTER_CTL(iface,
519 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200520 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200521 write_MASTER_CTL(iface,
522 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200523 iface->manual_stop = 1;
524 }
525 iface->writeNum--;
526 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200527 write_XMT_DATA8(iface, iface->command);
528 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200529 }
530 } else {
531 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200532 write_MASTER_CTL(iface,
533 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200534 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200535 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200536 iface->manual_stop = 1;
537 } else {
538 del_timer(&iface->timeout_timer);
539 break;
540 }
541 }
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
556 wait_for_completion(&iface->complete);
557
558 rc = (iface->result >= 0) ? 0 : -1;
559
Bryan Wud24ecfc2007-05-01 23:26:32 +0200560 return rc;
561}
562
563/*
564 * Return what the adapter supports
565 */
566static u32 bfin_twi_functionality(struct i2c_adapter *adap)
567{
568 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
569 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
570 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
571 I2C_FUNC_I2C;
572}
573
574
575static struct i2c_algorithm bfin_twi_algorithm = {
576 .master_xfer = bfin_twi_master_xfer,
577 .smbus_xfer = bfin_twi_smbus_xfer,
578 .functionality = bfin_twi_functionality,
579};
580
581
582static int i2c_bfin_twi_suspend(struct platform_device *dev, pm_message_t state)
583{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200584 struct bfin_twi_iface *iface = platform_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200585
586 /* Disable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200587 write_CONTROL(iface, read_CONTROL(iface) & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200588 SSYNC();
589
590 return 0;
591}
592
593static int i2c_bfin_twi_resume(struct platform_device *dev)
594{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200595 struct bfin_twi_iface *iface = platform_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200596
597 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200598 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200599 SSYNC();
600
601 return 0;
602}
603
Bryan Wuaa3d0202008-04-22 22:16:48 +0200604static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200605{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200606 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200607 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200608 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200609 int rc;
610
Bryan Wuaa3d0202008-04-22 22:16:48 +0200611 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
612 if (!iface) {
613 dev_err(&pdev->dev, "Cannot allocate memory\n");
614 rc = -ENOMEM;
615 goto out_error_nomem;
616 }
617
Bryan Wud24ecfc2007-05-01 23:26:32 +0200618 spin_lock_init(&(iface->lock));
619 init_completion(&(iface->complete));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200620
621 /* Find and map our resources */
622 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
623 if (res == NULL) {
624 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
625 rc = -ENOENT;
626 goto out_error_get_res;
627 }
628
629 iface->regs_base = ioremap(res->start, res->end - res->start + 1);
630 if (iface->regs_base == NULL) {
631 dev_err(&pdev->dev, "Cannot map IO\n");
632 rc = -ENXIO;
633 goto out_error_ioremap;
634 }
635
636 iface->irq = platform_get_irq(pdev, 0);
637 if (iface->irq < 0) {
638 dev_err(&pdev->dev, "No IRQ specified\n");
639 rc = -ENOENT;
640 goto out_error_no_irq;
641 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200642
643 init_timer(&(iface->timeout_timer));
644 iface->timeout_timer.function = bfin_twi_timeout;
645 iface->timeout_timer.data = (unsigned long)iface;
646
647 p_adap = &iface->adap;
648 p_adap->id = I2C_HW_BLACKFIN;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200649 p_adap->nr = pdev->id;
650 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200651 p_adap->algo = &bfin_twi_algorithm;
652 p_adap->algo_data = iface;
653 p_adap->class = I2C_CLASS_ALL;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200654 p_adap->dev.parent = &pdev->dev;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200655
656 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200657 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200658 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200659 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
660 rc = -ENODEV;
661 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200662 }
663
664 /* Set TWI internal clock as 10MHz */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200665 write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200666
667 /* Set Twi interface clock as specified */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200668 write_CLKDIV(iface, ((5*1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ)
669 << 8) | ((5*1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200670 & 0xFF));
671
672 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200673 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200674 SSYNC();
675
Kalle Pokki991dee52008-01-27 18:14:52 +0100676 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200677 if (rc < 0) {
678 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
679 goto out_error_add_adapter;
680 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200681
Bryan Wuaa3d0202008-04-22 22:16:48 +0200682 platform_set_drvdata(pdev, iface);
683
684 dev_info(&pdev->dev, "Blackfin I2C TWI controller, regs_base@%p\n",
685 iface->regs_base);
686
687 return 0;
688
689out_error_add_adapter:
690 free_irq(iface->irq, iface);
691out_error_req_irq:
692out_error_no_irq:
693 iounmap(iface->regs_base);
694out_error_ioremap:
695out_error_get_res:
696 kfree(iface);
697out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200698 return rc;
699}
700
701static int i2c_bfin_twi_remove(struct platform_device *pdev)
702{
703 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
704
705 platform_set_drvdata(pdev, NULL);
706
707 i2c_del_adapter(&(iface->adap));
708 free_irq(iface->irq, iface);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200709 iounmap(iface->regs_base);
710 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200711
712 return 0;
713}
714
715static struct platform_driver i2c_bfin_twi_driver = {
716 .probe = i2c_bfin_twi_probe,
717 .remove = i2c_bfin_twi_remove,
718 .suspend = i2c_bfin_twi_suspend,
719 .resume = i2c_bfin_twi_resume,
720 .driver = {
721 .name = "i2c-bfin-twi",
722 .owner = THIS_MODULE,
723 },
724};
725
726static int __init i2c_bfin_twi_init(void)
727{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200728 return platform_driver_register(&i2c_bfin_twi_driver);
729}
730
731static void __exit i2c_bfin_twi_exit(void)
732{
733 platform_driver_unregister(&i2c_bfin_twi_driver);
734}
735
736MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
737MODULE_DESCRIPTION("I2C-Bus adapter routines for Blackfin TWI");
738MODULE_LICENSE("GPL");
739
740module_init(i2c_bfin_twi_init);
741module_exit(i2c_bfin_twi_exit);