blob: 4d73ad7b5703901e0032ce5d562566f736a5b0d3 [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>
15#include <linux/mm.h>
16#include <linux/timer.h>
17#include <linux/spinlock.h>
18#include <linux/completion.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21
22#include <asm/blackfin.h>
Bryan Wu74d362e2008-04-22 22:16:48 +020023#include <asm/portmux.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020024#include <asm/irq.h>
25
26#define POLL_TIMEOUT (2 * HZ)
27
28/* 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;
45 int timeout_count;
46 struct timer_list timeout_timer;
47 struct i2c_adapter adap;
48 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020049 struct i2c_msg *pmsg;
50 int msg_num;
51 int cur_msg;
Michael Hennerich958585f2008-07-27 14:41:54 +080052 u16 saved_clkdiv;
53 u16 saved_control;
Bryan Wuaa3d0202008-04-22 22:16:48 +020054 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020055};
56
Bryan Wuaa3d0202008-04-22 22:16:48 +020057
58#define DEFINE_TWI_REG(reg, off) \
59static inline u16 read_##reg(struct bfin_twi_iface *iface) \
60 { return bfin_read16(iface->regs_base + (off)); } \
61static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
62 { bfin_write16(iface->regs_base + (off), v); }
63
64DEFINE_TWI_REG(CLKDIV, 0x00)
65DEFINE_TWI_REG(CONTROL, 0x04)
66DEFINE_TWI_REG(SLAVE_CTL, 0x08)
67DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
68DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
69DEFINE_TWI_REG(MASTER_CTL, 0x14)
70DEFINE_TWI_REG(MASTER_STAT, 0x18)
71DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
72DEFINE_TWI_REG(INT_STAT, 0x20)
73DEFINE_TWI_REG(INT_MASK, 0x24)
74DEFINE_TWI_REG(FIFO_CTL, 0x28)
75DEFINE_TWI_REG(FIFO_STAT, 0x2C)
76DEFINE_TWI_REG(XMT_DATA8, 0x80)
77DEFINE_TWI_REG(XMT_DATA16, 0x84)
78DEFINE_TWI_REG(RCV_DATA8, 0x88)
79DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020080
Bryan Wu74d362e2008-04-22 22:16:48 +020081static const u16 pin_req[2][3] = {
82 {P_TWI0_SCL, P_TWI0_SDA, 0},
83 {P_TWI1_SCL, P_TWI1_SDA, 0},
84};
85
Bryan Wud24ecfc2007-05-01 23:26:32 +020086static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
87{
Bryan Wuaa3d0202008-04-22 22:16:48 +020088 unsigned short twi_int_status = read_INT_STAT(iface);
89 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020090
91 if (twi_int_status & XMTSERV) {
92 /* Transmit next data */
93 if (iface->writeNum > 0) {
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 &&
107 iface->cur_msg+1 < iface->msg_num)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200108 write_MASTER_CTL(iface,
109 read_MASTER_CTL(iface) | RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200110 SSYNC();
111 /* Clear status */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200112 write_INT_STAT(iface, XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200113 SSYNC();
114 }
115 if (twi_int_status & RCVSERV) {
116 if (iface->readNum > 0) {
117 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200118 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200119 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
120 /* Change combine mode into sub mode after
121 * read first data.
122 */
123 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
124 /* Get read number from first byte in block
125 * combine mode.
126 */
127 if (iface->readNum == 1 && iface->manual_stop)
128 iface->readNum = *iface->transPtr + 1;
129 }
130 iface->transPtr++;
131 iface->readNum--;
132 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200133 write_MASTER_CTL(iface,
134 read_MASTER_CTL(iface) | STOP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200135 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200136 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
137 iface->cur_msg+1 < iface->msg_num) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200138 write_MASTER_CTL(iface,
139 read_MASTER_CTL(iface) | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200140 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200141 }
142 /* Clear interrupt source */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200143 write_INT_STAT(iface, RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200144 SSYNC();
145 }
146 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200147 write_INT_STAT(iface, MERR);
148 write_INT_MASK(iface, 0);
149 write_MASTER_STAT(iface, 0x3e);
150 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200151 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200152 iface->result = -EIO;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200153 /* if both err and complete int stats are set, return proper
154 * results.
155 */
156 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200157 write_INT_STAT(iface, MCOMP);
158 write_INT_MASK(iface, 0);
159 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200160 SSYNC();
161 /* If it is a quick transfer, only address bug no data,
162 * not an err, return 1.
163 */
164 if (iface->writeNum == 0 && (mast_stat & BUFRDERR))
165 iface->result = 1;
166 /* If address not acknowledged return -1,
167 * else return 0.
168 */
169 else if (!(mast_stat & ANAK))
170 iface->result = 0;
171 }
172 complete(&iface->complete);
173 return;
174 }
175 if (twi_int_status & MCOMP) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200176 write_INT_STAT(iface, MCOMP);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200177 SSYNC();
178 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
179 if (iface->readNum == 0) {
180 /* set the read number to 1 and ask for manual
181 * stop in block combine mode
182 */
183 iface->readNum = 1;
184 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200185 write_MASTER_CTL(iface,
186 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200187 } else {
188 /* set the readd number in other
189 * combine mode.
190 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200191 write_MASTER_CTL(iface,
192 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200193 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200194 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200195 }
196 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200197 write_MASTER_CTL(iface,
198 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200199 SSYNC();
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200200 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
201 iface->cur_msg+1 < iface->msg_num) {
202 iface->cur_msg++;
203 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
204 iface->writeNum = iface->readNum =
205 iface->pmsg[iface->cur_msg].len;
206 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200207 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200208 iface->pmsg[iface->cur_msg].addr);
209 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
210 iface->read_write = I2C_SMBUS_READ;
211 else {
212 iface->read_write = I2C_SMBUS_WRITE;
213 /* Transmit first data */
214 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200215 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200216 *(iface->transPtr++));
217 iface->writeNum--;
218 SSYNC();
219 }
220 }
221
222 if (iface->pmsg[iface->cur_msg].len <= 255)
Sonic Zhang57a8f322009-05-19 07:21:58 -0400223 write_MASTER_CTL(iface,
224 (read_MASTER_CTL(iface) &
225 (~(0xff << 6))) |
226 (iface->pmsg[iface->cur_msg].len << 6));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200227 else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400228 write_MASTER_CTL(iface,
229 (read_MASTER_CTL(iface) |
230 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200231 iface->manual_stop = 1;
232 }
233 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200234 write_MASTER_CTL(iface,
235 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200236 SSYNC();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200237 } else {
238 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200239 write_INT_MASK(iface, 0);
240 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200241 SSYNC();
242 complete(&iface->complete);
243 }
244 }
245}
246
247/* Interrupt handler */
248static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
249{
250 struct bfin_twi_iface *iface = dev_id;
251 unsigned long flags;
252
253 spin_lock_irqsave(&iface->lock, flags);
254 del_timer(&iface->timeout_timer);
255 bfin_twi_handle_interrupt(iface);
256 spin_unlock_irqrestore(&iface->lock, flags);
257 return IRQ_HANDLED;
258}
259
260static void bfin_twi_timeout(unsigned long data)
261{
262 struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data;
263 unsigned long flags;
264
265 spin_lock_irqsave(&iface->lock, flags);
266 bfin_twi_handle_interrupt(iface);
267 if (iface->result == 0) {
268 iface->timeout_count--;
269 if (iface->timeout_count > 0) {
270 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
271 add_timer(&iface->timeout_timer);
272 } else {
273 iface->result = -1;
274 complete(&iface->complete);
275 }
276 }
277 spin_unlock_irqrestore(&iface->lock, flags);
278}
279
280/*
281 * Generic i2c master transfer entrypoint
282 */
283static int bfin_twi_master_xfer(struct i2c_adapter *adap,
284 struct i2c_msg *msgs, int num)
285{
286 struct bfin_twi_iface *iface = adap->algo_data;
287 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200288 int rc = 0;
289
Bryan Wuaa3d0202008-04-22 22:16:48 +0200290 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200291 return -ENXIO;
292
Bryan Wuaa3d0202008-04-22 22:16:48 +0200293 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200294 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200295
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200296 iface->pmsg = msgs;
297 iface->msg_num = num;
298 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200299
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200300 pmsg = &msgs[0];
301 if (pmsg->flags & I2C_M_TEN) {
302 dev_err(&adap->dev, "10 bits addr not supported!\n");
303 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200304 }
305
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200306 iface->cur_mode = TWI_I2C_MODE_REPEAT;
307 iface->manual_stop = 0;
308 iface->transPtr = pmsg->buf;
309 iface->writeNum = iface->readNum = pmsg->len;
310 iface->result = 0;
311 iface->timeout_count = 10;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200312 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200313 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200314 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200315
316 /* FIFO Initiation. Data in FIFO should be
317 * discarded before start a new operation.
318 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200319 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200320 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200321 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200322 SSYNC();
323
324 if (pmsg->flags & I2C_M_RD)
325 iface->read_write = I2C_SMBUS_READ;
326 else {
327 iface->read_write = I2C_SMBUS_WRITE;
328 /* Transmit first data */
329 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200330 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200331 iface->writeNum--;
332 SSYNC();
333 }
334 }
335
336 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200337 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200338
339 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200340 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200341 SSYNC();
342
343 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200344 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200345 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200346 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200347 iface->manual_stop = 1;
348 }
349
350 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
351 add_timer(&iface->timeout_timer);
352
353 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200354 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200355 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
356 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
357 SSYNC();
358
359 wait_for_completion(&iface->complete);
360
361 rc = iface->result;
362
363 if (rc == 1)
364 return num;
365 else
366 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200367}
368
369/*
370 * SMBus type transfer entrypoint
371 */
372
373int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
374 unsigned short flags, char read_write,
375 u8 command, int size, union i2c_smbus_data *data)
376{
377 struct bfin_twi_iface *iface = adap->algo_data;
378 int rc = 0;
379
Bryan Wuaa3d0202008-04-22 22:16:48 +0200380 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200381 return -ENXIO;
382
Bryan Wuaa3d0202008-04-22 22:16:48 +0200383 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200384 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200385
386 iface->writeNum = 0;
387 iface->readNum = 0;
388
389 /* Prepare datas & select mode */
390 switch (size) {
391 case I2C_SMBUS_QUICK:
392 iface->transPtr = NULL;
393 iface->cur_mode = TWI_I2C_MODE_STANDARD;
394 break;
395 case I2C_SMBUS_BYTE:
396 if (data == NULL)
397 iface->transPtr = NULL;
398 else {
399 if (read_write == I2C_SMBUS_READ)
400 iface->readNum = 1;
401 else
402 iface->writeNum = 1;
403 iface->transPtr = &data->byte;
404 }
405 iface->cur_mode = TWI_I2C_MODE_STANDARD;
406 break;
407 case I2C_SMBUS_BYTE_DATA:
408 if (read_write == I2C_SMBUS_READ) {
409 iface->readNum = 1;
410 iface->cur_mode = TWI_I2C_MODE_COMBINED;
411 } else {
412 iface->writeNum = 1;
413 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
414 }
415 iface->transPtr = &data->byte;
416 break;
417 case I2C_SMBUS_WORD_DATA:
418 if (read_write == I2C_SMBUS_READ) {
419 iface->readNum = 2;
420 iface->cur_mode = TWI_I2C_MODE_COMBINED;
421 } else {
422 iface->writeNum = 2;
423 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
424 }
425 iface->transPtr = (u8 *)&data->word;
426 break;
427 case I2C_SMBUS_PROC_CALL:
428 iface->writeNum = 2;
429 iface->readNum = 2;
430 iface->cur_mode = TWI_I2C_MODE_COMBINED;
431 iface->transPtr = (u8 *)&data->word;
432 break;
433 case I2C_SMBUS_BLOCK_DATA:
434 if (read_write == I2C_SMBUS_READ) {
435 iface->readNum = 0;
436 iface->cur_mode = TWI_I2C_MODE_COMBINED;
437 } else {
438 iface->writeNum = data->block[0] + 1;
439 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
440 }
441 iface->transPtr = data->block;
442 break;
443 default:
444 return -1;
445 }
446
447 iface->result = 0;
448 iface->manual_stop = 0;
449 iface->read_write = read_write;
450 iface->command = command;
451 iface->timeout_count = 10;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200452 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200453
454 /* FIFO Initiation. Data in FIFO should be discarded before
455 * start a new operation.
456 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200457 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200458 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200459 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200460
461 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200462 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200463
464 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200465 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200466 SSYNC();
467
468 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
469 add_timer(&iface->timeout_timer);
470
471 switch (iface->cur_mode) {
472 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200473 write_XMT_DATA8(iface, iface->command);
474 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200475 ((iface->read_write == I2C_SMBUS_READ) ?
476 RCVSERV : XMTSERV));
477 SSYNC();
478
479 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200480 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200481 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200482 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200483 iface->manual_stop = 1;
484 }
485 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200486 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200487 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
488 break;
489 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200490 write_XMT_DATA8(iface, iface->command);
491 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200492 SSYNC();
493
494 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200495 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200496 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200498 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200499 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200500 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
501 break;
502 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200503 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200504 if (size != I2C_SMBUS_QUICK) {
505 /* Don't access xmit data register when this is a
506 * read operation.
507 */
508 if (iface->read_write != I2C_SMBUS_READ) {
509 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200510 write_XMT_DATA8(iface,
511 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200512 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200513 write_MASTER_CTL(iface,
514 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200515 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200516 write_MASTER_CTL(iface,
517 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200518 iface->manual_stop = 1;
519 }
520 iface->writeNum--;
521 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200522 write_XMT_DATA8(iface, iface->command);
523 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200524 }
525 } else {
526 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200527 write_MASTER_CTL(iface,
528 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200529 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200530 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200531 iface->manual_stop = 1;
532 } else {
533 del_timer(&iface->timeout_timer);
534 break;
535 }
536 }
537 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200538 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200539 ((iface->read_write == I2C_SMBUS_READ) ?
540 RCVSERV : XMTSERV));
541 SSYNC();
542
543 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200544 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200545 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
546 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
547 break;
548 }
549 SSYNC();
550
551 wait_for_completion(&iface->complete);
552
553 rc = (iface->result >= 0) ? 0 : -1;
554
Bryan Wud24ecfc2007-05-01 23:26:32 +0200555 return rc;
556}
557
558/*
559 * Return what the adapter supports
560 */
561static u32 bfin_twi_functionality(struct i2c_adapter *adap)
562{
563 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
564 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
565 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
566 I2C_FUNC_I2C;
567}
568
Bryan Wud24ecfc2007-05-01 23:26:32 +0200569static struct i2c_algorithm bfin_twi_algorithm = {
570 .master_xfer = bfin_twi_master_xfer,
571 .smbus_xfer = bfin_twi_smbus_xfer,
572 .functionality = bfin_twi_functionality,
573};
574
Michael Hennerich958585f2008-07-27 14:41:54 +0800575static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200576{
Michael Hennerich958585f2008-07-27 14:41:54 +0800577 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
578
579 iface->saved_clkdiv = read_CLKDIV(iface);
580 iface->saved_control = read_CONTROL(iface);
581
582 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200583
584 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800585 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200586
587 return 0;
588}
589
Michael Hennerich958585f2008-07-27 14:41:54 +0800590static int i2c_bfin_twi_resume(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200591{
Michael Hennerich958585f2008-07-27 14:41:54 +0800592 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200593
Michael Hennerich958585f2008-07-27 14:41:54 +0800594 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
595 IRQF_DISABLED, pdev->name, iface);
596 if (rc) {
597 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
598 return -ENODEV;
599 }
600
601 /* Resume TWI interface clock as specified */
602 write_CLKDIV(iface, iface->saved_clkdiv);
603
604 /* Resume TWI */
605 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200606
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;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400616 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200617
Bryan Wuaa3d0202008-04-22 22:16:48 +0200618 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
619 if (!iface) {
620 dev_err(&pdev->dev, "Cannot allocate memory\n");
621 rc = -ENOMEM;
622 goto out_error_nomem;
623 }
624
Bryan Wud24ecfc2007-05-01 23:26:32 +0200625 spin_lock_init(&(iface->lock));
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;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200654 p_adap->nr = pdev->id;
655 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200656 p_adap->algo = &bfin_twi_algorithm;
657 p_adap->algo_data = iface;
Jean Delvaree1995f62009-01-07 14:29:16 +0100658 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200659 p_adap->dev.parent = &pdev->dev;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200660
Bryan Wu74d362e2008-04-22 22:16:48 +0200661 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
662 if (rc) {
663 dev_err(&pdev->dev, "Can't setup pin mux!\n");
664 goto out_error_pin_mux;
665 }
666
Bryan Wud24ecfc2007-05-01 23:26:32 +0200667 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Bryan Wuaa3d0202008-04-22 22:16:48 +0200668 IRQF_DISABLED, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200669 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200670 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
671 rc = -ENODEV;
672 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200673 }
674
675 /* Set TWI internal clock as 10MHz */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200676 write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200677
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400678 /*
679 * We will not end up with a CLKDIV=0 because no one will specify
680 * 20kHz SCL or less in Kconfig now. (5 * 1024 / 20 = 0x100)
681 */
682 clkhilow = 5 * 1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ;
683
Bryan Wud24ecfc2007-05-01 23:26:32 +0200684 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400685 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200686
687 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200688 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200689 SSYNC();
690
Kalle Pokki991dee52008-01-27 18:14:52 +0100691 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200692 if (rc < 0) {
693 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
694 goto out_error_add_adapter;
695 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200696
Bryan Wuaa3d0202008-04-22 22:16:48 +0200697 platform_set_drvdata(pdev, iface);
698
Bryan Wufa6ad222008-04-22 22:16:48 +0200699 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
700 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200701
702 return 0;
703
704out_error_add_adapter:
705 free_irq(iface->irq, iface);
706out_error_req_irq:
707out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200708 peripheral_free_list(pin_req[pdev->id]);
709out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200710 iounmap(iface->regs_base);
711out_error_ioremap:
712out_error_get_res:
713 kfree(iface);
714out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200715 return rc;
716}
717
718static int i2c_bfin_twi_remove(struct platform_device *pdev)
719{
720 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
721
722 platform_set_drvdata(pdev, NULL);
723
724 i2c_del_adapter(&(iface->adap));
725 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200726 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200727 iounmap(iface->regs_base);
728 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200729
730 return 0;
731}
732
733static struct platform_driver i2c_bfin_twi_driver = {
734 .probe = i2c_bfin_twi_probe,
735 .remove = i2c_bfin_twi_remove,
736 .suspend = i2c_bfin_twi_suspend,
737 .resume = i2c_bfin_twi_resume,
738 .driver = {
739 .name = "i2c-bfin-twi",
740 .owner = THIS_MODULE,
741 },
742};
743
744static int __init i2c_bfin_twi_init(void)
745{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200746 return platform_driver_register(&i2c_bfin_twi_driver);
747}
748
749static void __exit i2c_bfin_twi_exit(void)
750{
751 platform_driver_unregister(&i2c_bfin_twi_driver);
752}
753
Bryan Wud24ecfc2007-05-01 23:26:32 +0200754module_init(i2c_bfin_twi_init);
755module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200756
757MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
758MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
759MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200760MODULE_ALIAS("platform:i2c-bfin-twi");