blob: 446b4f855b034826cdf3b4026a4f88af7748842d [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>
Bryan Wu74d362e2008-04-22 22:16:48 +020037#include <asm/portmux.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020038#include <asm/irq.h>
39
40#define POLL_TIMEOUT (2 * HZ)
41
42/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020043#define TWI_I2C_MODE_STANDARD 1
44#define TWI_I2C_MODE_STANDARDSUB 2
45#define TWI_I2C_MODE_COMBINED 3
46#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020047
48struct bfin_twi_iface {
Bryan Wud24ecfc2007-05-01 23:26:32 +020049 int irq;
50 spinlock_t lock;
51 char read_write;
52 u8 command;
53 u8 *transPtr;
54 int readNum;
55 int writeNum;
56 int cur_mode;
57 int manual_stop;
58 int result;
59 int timeout_count;
60 struct timer_list timeout_timer;
61 struct i2c_adapter adap;
62 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020063 struct i2c_msg *pmsg;
64 int msg_num;
65 int cur_msg;
Bryan Wuaa3d0202008-04-22 22:16:48 +020066 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020067};
68
Bryan Wuaa3d0202008-04-22 22:16:48 +020069
70#define DEFINE_TWI_REG(reg, off) \
71static inline u16 read_##reg(struct bfin_twi_iface *iface) \
72 { return bfin_read16(iface->regs_base + (off)); } \
73static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
74 { bfin_write16(iface->regs_base + (off), v); }
75
76DEFINE_TWI_REG(CLKDIV, 0x00)
77DEFINE_TWI_REG(CONTROL, 0x04)
78DEFINE_TWI_REG(SLAVE_CTL, 0x08)
79DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
80DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
81DEFINE_TWI_REG(MASTER_CTL, 0x14)
82DEFINE_TWI_REG(MASTER_STAT, 0x18)
83DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
84DEFINE_TWI_REG(INT_STAT, 0x20)
85DEFINE_TWI_REG(INT_MASK, 0x24)
86DEFINE_TWI_REG(FIFO_CTL, 0x28)
87DEFINE_TWI_REG(FIFO_STAT, 0x2C)
88DEFINE_TWI_REG(XMT_DATA8, 0x80)
89DEFINE_TWI_REG(XMT_DATA16, 0x84)
90DEFINE_TWI_REG(RCV_DATA8, 0x88)
91DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020092
Bryan Wu74d362e2008-04-22 22:16:48 +020093static const u16 pin_req[2][3] = {
94 {P_TWI0_SCL, P_TWI0_SDA, 0},
95 {P_TWI1_SCL, P_TWI1_SDA, 0},
96};
97
Bryan Wud24ecfc2007-05-01 23:26:32 +020098static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
99{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200100 unsigned short twi_int_status = read_INT_STAT(iface);
101 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200102
103 if (twi_int_status & XMTSERV) {
104 /* Transmit next data */
105 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200106 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200107 iface->writeNum--;
108 }
109 /* start receive immediately after complete sending in
110 * combine mode.
111 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200112 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200113 write_MASTER_CTL(iface,
114 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200115 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200116 write_MASTER_CTL(iface,
117 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200118 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
119 iface->cur_msg+1 < iface->msg_num)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200120 write_MASTER_CTL(iface,
121 read_MASTER_CTL(iface) | RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200122 SSYNC();
123 /* Clear status */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200124 write_INT_STAT(iface, XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200125 SSYNC();
126 }
127 if (twi_int_status & RCVSERV) {
128 if (iface->readNum > 0) {
129 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200130 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200131 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
132 /* Change combine mode into sub mode after
133 * read first data.
134 */
135 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
136 /* Get read number from first byte in block
137 * combine mode.
138 */
139 if (iface->readNum == 1 && iface->manual_stop)
140 iface->readNum = *iface->transPtr + 1;
141 }
142 iface->transPtr++;
143 iface->readNum--;
144 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200145 write_MASTER_CTL(iface,
146 read_MASTER_CTL(iface) | STOP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200147 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200148 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
149 iface->cur_msg+1 < iface->msg_num) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200150 write_MASTER_CTL(iface,
151 read_MASTER_CTL(iface) | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200152 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200153 }
154 /* Clear interrupt source */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200155 write_INT_STAT(iface, RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200156 SSYNC();
157 }
158 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200159 write_INT_STAT(iface, MERR);
160 write_INT_MASK(iface, 0);
161 write_MASTER_STAT(iface, 0x3e);
162 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200163 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200164 iface->result = -EIO;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200165 /* if both err and complete int stats are set, return proper
166 * results.
167 */
168 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200169 write_INT_STAT(iface, MCOMP);
170 write_INT_MASK(iface, 0);
171 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200172 SSYNC();
173 /* If it is a quick transfer, only address bug no data,
174 * not an err, return 1.
175 */
176 if (iface->writeNum == 0 && (mast_stat & BUFRDERR))
177 iface->result = 1;
178 /* If address not acknowledged return -1,
179 * else return 0.
180 */
181 else if (!(mast_stat & ANAK))
182 iface->result = 0;
183 }
184 complete(&iface->complete);
185 return;
186 }
187 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200188 write_INT_STAT(iface, MCOMP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200189 SSYNC();
190 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
191 if (iface->readNum == 0) {
192 /* set the read number to 1 and ask for manual
193 * stop in block combine mode
194 */
195 iface->readNum = 1;
196 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200197 write_MASTER_CTL(iface,
198 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200199 } else {
200 /* set the readd number in other
201 * combine mode.
202 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200203 write_MASTER_CTL(iface,
204 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200205 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200206 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200207 }
208 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200209 write_MASTER_CTL(iface,
210 read_MASTER_CTL(iface) & ~RSTART);
211 write_MASTER_CTL(iface,
212 read_MASTER_CTL(iface) | MEN | MDIR);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200213 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200214 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
215 iface->cur_msg+1 < iface->msg_num) {
216 iface->cur_msg++;
217 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
218 iface->writeNum = iface->readNum =
219 iface->pmsg[iface->cur_msg].len;
220 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200221 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200222 iface->pmsg[iface->cur_msg].addr);
223 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
224 iface->read_write = I2C_SMBUS_READ;
225 else {
226 iface->read_write = I2C_SMBUS_WRITE;
227 /* Transmit first data */
228 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200229 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200230 *(iface->transPtr++));
231 iface->writeNum--;
232 SSYNC();
233 }
234 }
235
236 if (iface->pmsg[iface->cur_msg].len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200237 write_MASTER_CTL(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200238 iface->pmsg[iface->cur_msg].len << 6);
239 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200240 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200241 iface->manual_stop = 1;
242 }
243 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200244 write_MASTER_CTL(iface,
245 read_MASTER_CTL(iface) & ~RSTART);
246 write_MASTER_CTL(iface, read_MASTER_CTL(iface) |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200247 MEN | ((iface->read_write == I2C_SMBUS_READ) ?
248 MDIR : 0));
249 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200250 } else {
251 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200252 write_INT_MASK(iface, 0);
253 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200254 SSYNC();
255 complete(&iface->complete);
256 }
257 }
258}
259
260/* Interrupt handler */
261static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
262{
263 struct bfin_twi_iface *iface = dev_id;
264 unsigned long flags;
265
266 spin_lock_irqsave(&iface->lock, flags);
267 del_timer(&iface->timeout_timer);
268 bfin_twi_handle_interrupt(iface);
269 spin_unlock_irqrestore(&iface->lock, flags);
270 return IRQ_HANDLED;
271}
272
273static void bfin_twi_timeout(unsigned long data)
274{
275 struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data;
276 unsigned long flags;
277
278 spin_lock_irqsave(&iface->lock, flags);
279 bfin_twi_handle_interrupt(iface);
280 if (iface->result == 0) {
281 iface->timeout_count--;
282 if (iface->timeout_count > 0) {
283 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
284 add_timer(&iface->timeout_timer);
285 } else {
286 iface->result = -1;
287 complete(&iface->complete);
288 }
289 }
290 spin_unlock_irqrestore(&iface->lock, flags);
291}
292
293/*
294 * Generic i2c master transfer entrypoint
295 */
296static int bfin_twi_master_xfer(struct i2c_adapter *adap,
297 struct i2c_msg *msgs, int num)
298{
299 struct bfin_twi_iface *iface = adap->algo_data;
300 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200301 int rc = 0;
302
Bryan Wuaa3d0202008-04-22 22:16:48 +0200303 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200304 return -ENXIO;
305
Bryan Wuaa3d0202008-04-22 22:16:48 +0200306 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200307 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200308
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200309 iface->pmsg = msgs;
310 iface->msg_num = num;
311 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200312
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200313 pmsg = &msgs[0];
314 if (pmsg->flags & I2C_M_TEN) {
315 dev_err(&adap->dev, "10 bits addr not supported!\n");
316 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200317 }
318
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200319 iface->cur_mode = TWI_I2C_MODE_REPEAT;
320 iface->manual_stop = 0;
321 iface->transPtr = pmsg->buf;
322 iface->writeNum = iface->readNum = pmsg->len;
323 iface->result = 0;
324 iface->timeout_count = 10;
325 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200326 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200327
328 /* FIFO Initiation. Data in FIFO should be
329 * discarded before start a new operation.
330 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200331 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200332 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200333 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200334 SSYNC();
335
336 if (pmsg->flags & I2C_M_RD)
337 iface->read_write = I2C_SMBUS_READ;
338 else {
339 iface->read_write = I2C_SMBUS_WRITE;
340 /* Transmit first data */
341 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200342 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200343 iface->writeNum--;
344 SSYNC();
345 }
346 }
347
348 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200349 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200350
351 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200352 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200353 SSYNC();
354
355 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200356 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200357 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200358 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200359 iface->manual_stop = 1;
360 }
361
362 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
363 add_timer(&iface->timeout_timer);
364
365 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200366 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200367 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
368 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
369 SSYNC();
370
371 wait_for_completion(&iface->complete);
372
373 rc = iface->result;
374
375 if (rc == 1)
376 return num;
377 else
378 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200379}
380
381/*
382 * SMBus type transfer entrypoint
383 */
384
385int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
386 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;
455 default:
456 return -1;
457 }
458
459 iface->result = 0;
460 iface->manual_stop = 0;
461 iface->read_write = read_write;
462 iface->command = command;
463 iface->timeout_count = 10;
464
465 /* FIFO Initiation. Data in FIFO should be discarded before
466 * start a new operation.
467 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200468 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200469 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200470 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200471
472 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200473 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200474
475 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200476 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200477 SSYNC();
478
479 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
480 add_timer(&iface->timeout_timer);
481
482 switch (iface->cur_mode) {
483 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200484 write_XMT_DATA8(iface, iface->command);
485 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200486 ((iface->read_write == I2C_SMBUS_READ) ?
487 RCVSERV : XMTSERV));
488 SSYNC();
489
490 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200491 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200492 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200493 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200494 iface->manual_stop = 1;
495 }
496 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200498 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
499 break;
500 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200501 write_XMT_DATA8(iface, iface->command);
502 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200503 SSYNC();
504
505 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200506 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200507 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200508 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200509 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200510 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200511 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
512 break;
513 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200514 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200515 if (size != I2C_SMBUS_QUICK) {
516 /* Don't access xmit data register when this is a
517 * read operation.
518 */
519 if (iface->read_write != I2C_SMBUS_READ) {
520 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200521 write_XMT_DATA8(iface,
522 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200523 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200524 write_MASTER_CTL(iface,
525 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200526 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200527 write_MASTER_CTL(iface,
528 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200529 iface->manual_stop = 1;
530 }
531 iface->writeNum--;
532 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200533 write_XMT_DATA8(iface, iface->command);
534 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200535 }
536 } else {
537 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200538 write_MASTER_CTL(iface,
539 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200540 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200541 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200542 iface->manual_stop = 1;
543 } else {
544 del_timer(&iface->timeout_timer);
545 break;
546 }
547 }
548 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200549 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200550 ((iface->read_write == I2C_SMBUS_READ) ?
551 RCVSERV : XMTSERV));
552 SSYNC();
553
554 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200555 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200556 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
557 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
558 break;
559 }
560 SSYNC();
561
562 wait_for_completion(&iface->complete);
563
564 rc = (iface->result >= 0) ? 0 : -1;
565
Bryan Wud24ecfc2007-05-01 23:26:32 +0200566 return rc;
567}
568
569/*
570 * Return what the adapter supports
571 */
572static u32 bfin_twi_functionality(struct i2c_adapter *adap)
573{
574 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
575 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
576 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
577 I2C_FUNC_I2C;
578}
579
580
581static struct i2c_algorithm bfin_twi_algorithm = {
582 .master_xfer = bfin_twi_master_xfer,
583 .smbus_xfer = bfin_twi_smbus_xfer,
584 .functionality = bfin_twi_functionality,
585};
586
587
588static int i2c_bfin_twi_suspend(struct platform_device *dev, pm_message_t state)
589{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200590 struct bfin_twi_iface *iface = platform_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200591
592 /* Disable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200593 write_CONTROL(iface, read_CONTROL(iface) & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200594 SSYNC();
595
596 return 0;
597}
598
599static int i2c_bfin_twi_resume(struct platform_device *dev)
600{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200601 struct bfin_twi_iface *iface = platform_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200602
603 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200604 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200605 SSYNC();
606
607 return 0;
608}
609
Bryan Wuaa3d0202008-04-22 22:16:48 +0200610static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200611{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200612 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200613 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200614 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200615 int rc;
616
Bryan Wuaa3d0202008-04-22 22:16:48 +0200617 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
618 if (!iface) {
619 dev_err(&pdev->dev, "Cannot allocate memory\n");
620 rc = -ENOMEM;
621 goto out_error_nomem;
622 }
623
Bryan Wud24ecfc2007-05-01 23:26:32 +0200624 spin_lock_init(&(iface->lock));
625 init_completion(&(iface->complete));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200626
627 /* Find and map our resources */
628 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
629 if (res == NULL) {
630 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
631 rc = -ENOENT;
632 goto out_error_get_res;
633 }
634
635 iface->regs_base = ioremap(res->start, res->end - res->start + 1);
636 if (iface->regs_base == NULL) {
637 dev_err(&pdev->dev, "Cannot map IO\n");
638 rc = -ENXIO;
639 goto out_error_ioremap;
640 }
641
642 iface->irq = platform_get_irq(pdev, 0);
643 if (iface->irq < 0) {
644 dev_err(&pdev->dev, "No IRQ specified\n");
645 rc = -ENOENT;
646 goto out_error_no_irq;
647 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200648
649 init_timer(&(iface->timeout_timer));
650 iface->timeout_timer.function = bfin_twi_timeout;
651 iface->timeout_timer.data = (unsigned long)iface;
652
653 p_adap = &iface->adap;
654 p_adap->id = I2C_HW_BLACKFIN;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200655 p_adap->nr = pdev->id;
656 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200657 p_adap->algo = &bfin_twi_algorithm;
658 p_adap->algo_data = iface;
659 p_adap->class = I2C_CLASS_ALL;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200660 p_adap->dev.parent = &pdev->dev;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200661
Bryan Wu74d362e2008-04-22 22:16:48 +0200662 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
663 if (rc) {
664 dev_err(&pdev->dev, "Can't setup pin mux!\n");
665 goto out_error_pin_mux;
666 }
667
Bryan Wud24ecfc2007-05-01 23:26:32 +0200668 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200669 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200670 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200671 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
672 rc = -ENODEV;
673 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200674 }
675
676 /* Set TWI internal clock as 10MHz */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200677 write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200678
679 /* Set Twi interface clock as specified */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200680 write_CLKDIV(iface, ((5*1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ)
681 << 8) | ((5*1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200682 & 0xFF));
683
684 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200685 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200686 SSYNC();
687
Kalle Pokki991dee52008-01-27 18:14:52 +0100688 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200689 if (rc < 0) {
690 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
691 goto out_error_add_adapter;
692 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200693
Bryan Wuaa3d0202008-04-22 22:16:48 +0200694 platform_set_drvdata(pdev, iface);
695
696 dev_info(&pdev->dev, "Blackfin I2C TWI controller, regs_base@%p\n",
697 iface->regs_base);
698
699 return 0;
700
701out_error_add_adapter:
702 free_irq(iface->irq, iface);
703out_error_req_irq:
704out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200705 peripheral_free_list(pin_req[pdev->id]);
706out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200707 iounmap(iface->regs_base);
708out_error_ioremap:
709out_error_get_res:
710 kfree(iface);
711out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200712 return rc;
713}
714
715static int i2c_bfin_twi_remove(struct platform_device *pdev)
716{
717 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
718
719 platform_set_drvdata(pdev, NULL);
720
721 i2c_del_adapter(&(iface->adap));
722 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200723 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200724 iounmap(iface->regs_base);
725 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200726
727 return 0;
728}
729
730static struct platform_driver i2c_bfin_twi_driver = {
731 .probe = i2c_bfin_twi_probe,
732 .remove = i2c_bfin_twi_remove,
733 .suspend = i2c_bfin_twi_suspend,
734 .resume = i2c_bfin_twi_resume,
735 .driver = {
736 .name = "i2c-bfin-twi",
737 .owner = THIS_MODULE,
738 },
739};
740
741static int __init i2c_bfin_twi_init(void)
742{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200743 return platform_driver_register(&i2c_bfin_twi_driver);
744}
745
746static void __exit i2c_bfin_twi_exit(void)
747{
748 platform_driver_unregister(&i2c_bfin_twi_driver);
749}
750
751MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
752MODULE_DESCRIPTION("I2C-Bus adapter routines for Blackfin TWI");
753MODULE_LICENSE("GPL");
754
755module_init(i2c_bfin_twi_init);
756module_exit(i2c_bfin_twi_exit);