2 * Blackfin On-Chip Two Wire Interface Driver
4 * Copyright 2005-2007 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/i2c.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>
22 #include <asm/blackfin.h>
23 #include <asm/portmux.h>
26 #define POLL_TIMEOUT (2 * HZ)
29 #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
34 struct bfin_twi_iface {
46 struct timer_list timeout_timer;
47 struct i2c_adapter adap;
48 struct completion complete;
54 void __iomem *regs_base;
58 #define DEFINE_TWI_REG(reg, off) \
59 static inline u16 read_##reg(struct bfin_twi_iface *iface) \
60 { return bfin_read16(iface->regs_base + (off)); } \
61 static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
62 { bfin_write16(iface->regs_base + (off), v); }
64 DEFINE_TWI_REG(CLKDIV, 0x00)
65 DEFINE_TWI_REG(CONTROL, 0x04)
66 DEFINE_TWI_REG(SLAVE_CTL, 0x08)
67 DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
68 DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
69 DEFINE_TWI_REG(MASTER_CTL, 0x14)
70 DEFINE_TWI_REG(MASTER_STAT, 0x18)
71 DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
72 DEFINE_TWI_REG(INT_STAT, 0x20)
73 DEFINE_TWI_REG(INT_MASK, 0x24)
74 DEFINE_TWI_REG(FIFO_CTL, 0x28)
75 DEFINE_TWI_REG(FIFO_STAT, 0x2C)
76 DEFINE_TWI_REG(XMT_DATA8, 0x80)
77 DEFINE_TWI_REG(XMT_DATA16, 0x84)
78 DEFINE_TWI_REG(RCV_DATA8, 0x88)
79 DEFINE_TWI_REG(RCV_DATA16, 0x8C)
81 static const u16 pin_req[2][3] = {
82 {P_TWI0_SCL, P_TWI0_SDA, 0},
83 {P_TWI1_SCL, P_TWI1_SDA, 0},
86 static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
88 unsigned short twi_int_status = read_INT_STAT(iface);
89 unsigned short mast_stat = read_MASTER_STAT(iface);
91 if (twi_int_status & XMTSERV) {
92 /* Transmit next data */
93 if (iface->writeNum > 0) {
94 write_XMT_DATA8(iface, *(iface->transPtr++));
97 /* start receive immediately after complete sending in
100 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
101 write_MASTER_CTL(iface,
102 read_MASTER_CTL(iface) | MDIR | RSTART);
103 else if (iface->manual_stop)
104 write_MASTER_CTL(iface,
105 read_MASTER_CTL(iface) | STOP);
106 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
107 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);
112 write_MASTER_CTL(iface,
113 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
117 write_INT_STAT(iface, XMTSERV);
120 if (twi_int_status & RCVSERV) {
121 if (iface->readNum > 0) {
122 /* Receive next data */
123 *(iface->transPtr) = read_RCV_DATA8(iface);
124 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
125 /* Change combine mode into sub mode after
128 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
129 /* Get read number from first byte in block
132 if (iface->readNum == 1 && iface->manual_stop)
133 iface->readNum = *iface->transPtr + 1;
137 } else if (iface->manual_stop) {
138 write_MASTER_CTL(iface,
139 read_MASTER_CTL(iface) | STOP);
141 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
142 iface->cur_msg + 1 < iface->msg_num) {
143 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
144 write_MASTER_CTL(iface,
145 read_MASTER_CTL(iface) | RSTART | MDIR);
147 write_MASTER_CTL(iface,
148 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
151 /* Clear interrupt source */
152 write_INT_STAT(iface, RCVSERV);
155 if (twi_int_status & MERR) {
156 write_INT_STAT(iface, MERR);
157 write_INT_MASK(iface, 0);
158 write_MASTER_STAT(iface, 0x3e);
159 write_MASTER_CTL(iface, 0);
161 iface->result = -EIO;
162 /* if both err and complete int stats are set, return proper
165 if (twi_int_status & MCOMP) {
166 write_INT_STAT(iface, MCOMP);
167 write_INT_MASK(iface, 0);
168 write_MASTER_CTL(iface, 0);
170 /* If it is a quick transfer, only address bug no data,
171 * not an err, return 1.
173 if (iface->writeNum == 0 && (mast_stat & BUFRDERR))
175 /* If address not acknowledged return -1,
178 else if (!(mast_stat & ANAK))
181 complete(&iface->complete);
184 if (twi_int_status & MCOMP) {
185 write_INT_STAT(iface, MCOMP);
187 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
188 if (iface->readNum == 0) {
189 /* set the read number to 1 and ask for manual
190 * stop in block combine mode
193 iface->manual_stop = 1;
194 write_MASTER_CTL(iface,
195 read_MASTER_CTL(iface) | (0xff << 6));
197 /* set the readd number in other
200 write_MASTER_CTL(iface,
201 (read_MASTER_CTL(iface) &
203 (iface->readNum << 6));
205 /* remove restart bit and enable master receive */
206 write_MASTER_CTL(iface,
207 read_MASTER_CTL(iface) & ~RSTART);
209 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
210 iface->cur_msg+1 < iface->msg_num) {
212 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
213 iface->writeNum = iface->readNum =
214 iface->pmsg[iface->cur_msg].len;
215 /* Set Transmit device address */
216 write_MASTER_ADDR(iface,
217 iface->pmsg[iface->cur_msg].addr);
218 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
219 iface->read_write = I2C_SMBUS_READ;
221 iface->read_write = I2C_SMBUS_WRITE;
222 /* Transmit first data */
223 if (iface->writeNum > 0) {
224 write_XMT_DATA8(iface,
225 *(iface->transPtr++));
231 if (iface->pmsg[iface->cur_msg].len <= 255)
232 write_MASTER_CTL(iface,
233 (read_MASTER_CTL(iface) &
235 (iface->pmsg[iface->cur_msg].len << 6));
237 write_MASTER_CTL(iface,
238 (read_MASTER_CTL(iface) |
240 iface->manual_stop = 1;
242 /* remove restart bit and enable master receive */
243 write_MASTER_CTL(iface,
244 read_MASTER_CTL(iface) & ~RSTART);
248 write_INT_MASK(iface, 0);
249 write_MASTER_CTL(iface, 0);
251 complete(&iface->complete);
256 /* Interrupt handler */
257 static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
259 struct bfin_twi_iface *iface = dev_id;
262 spin_lock_irqsave(&iface->lock, flags);
263 del_timer(&iface->timeout_timer);
264 bfin_twi_handle_interrupt(iface);
265 spin_unlock_irqrestore(&iface->lock, flags);
269 static void bfin_twi_timeout(unsigned long data)
271 struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data;
274 spin_lock_irqsave(&iface->lock, flags);
275 bfin_twi_handle_interrupt(iface);
276 if (iface->result == 0) {
277 iface->timeout_count--;
278 if (iface->timeout_count > 0) {
279 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
280 add_timer(&iface->timeout_timer);
283 complete(&iface->complete);
286 spin_unlock_irqrestore(&iface->lock, flags);
290 * Generic i2c master transfer entrypoint
292 static int bfin_twi_master_xfer(struct i2c_adapter *adap,
293 struct i2c_msg *msgs, int num)
295 struct bfin_twi_iface *iface = adap->algo_data;
296 struct i2c_msg *pmsg;
299 if (!(read_CONTROL(iface) & TWI_ENA))
302 while (read_MASTER_STAT(iface) & BUSBUSY)
306 iface->msg_num = num;
310 if (pmsg->flags & I2C_M_TEN) {
311 dev_err(&adap->dev, "10 bits addr not supported!\n");
315 iface->cur_mode = TWI_I2C_MODE_REPEAT;
316 iface->manual_stop = 0;
317 iface->transPtr = pmsg->buf;
318 iface->writeNum = iface->readNum = pmsg->len;
320 iface->timeout_count = 10;
321 init_completion(&(iface->complete));
322 /* Set Transmit device address */
323 write_MASTER_ADDR(iface, pmsg->addr);
325 /* FIFO Initiation. Data in FIFO should be
326 * discarded before start a new operation.
328 write_FIFO_CTL(iface, 0x3);
330 write_FIFO_CTL(iface, 0);
333 if (pmsg->flags & I2C_M_RD)
334 iface->read_write = I2C_SMBUS_READ;
336 iface->read_write = I2C_SMBUS_WRITE;
337 /* Transmit first data */
338 if (iface->writeNum > 0) {
339 write_XMT_DATA8(iface, *(iface->transPtr++));
346 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
348 /* Interrupt mask . Enable XMT, RCV interrupt */
349 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
352 if (pmsg->len <= 255)
353 write_MASTER_CTL(iface, pmsg->len << 6);
355 write_MASTER_CTL(iface, 0xff << 6);
356 iface->manual_stop = 1;
359 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
360 add_timer(&iface->timeout_timer);
363 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
364 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
365 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
368 wait_for_completion(&iface->complete);
379 * SMBus type transfer entrypoint
382 int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
383 unsigned short flags, char read_write,
384 u8 command, int size, union i2c_smbus_data *data)
386 struct bfin_twi_iface *iface = adap->algo_data;
389 if (!(read_CONTROL(iface) & TWI_ENA))
392 while (read_MASTER_STAT(iface) & BUSBUSY)
398 /* Prepare datas & select mode */
400 case I2C_SMBUS_QUICK:
401 iface->transPtr = NULL;
402 iface->cur_mode = TWI_I2C_MODE_STANDARD;
406 iface->transPtr = NULL;
408 if (read_write == I2C_SMBUS_READ)
412 iface->transPtr = &data->byte;
414 iface->cur_mode = TWI_I2C_MODE_STANDARD;
416 case I2C_SMBUS_BYTE_DATA:
417 if (read_write == I2C_SMBUS_READ) {
419 iface->cur_mode = TWI_I2C_MODE_COMBINED;
422 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
424 iface->transPtr = &data->byte;
426 case I2C_SMBUS_WORD_DATA:
427 if (read_write == I2C_SMBUS_READ) {
429 iface->cur_mode = TWI_I2C_MODE_COMBINED;
432 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
434 iface->transPtr = (u8 *)&data->word;
436 case I2C_SMBUS_PROC_CALL:
439 iface->cur_mode = TWI_I2C_MODE_COMBINED;
440 iface->transPtr = (u8 *)&data->word;
442 case I2C_SMBUS_BLOCK_DATA:
443 if (read_write == I2C_SMBUS_READ) {
445 iface->cur_mode = TWI_I2C_MODE_COMBINED;
447 iface->writeNum = data->block[0] + 1;
448 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
450 iface->transPtr = data->block;
452 case I2C_SMBUS_I2C_BLOCK_DATA:
453 if (read_write == I2C_SMBUS_READ) {
454 iface->readNum = data->block[0];
455 iface->cur_mode = TWI_I2C_MODE_COMBINED;
457 iface->writeNum = data->block[0];
458 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
460 iface->transPtr = (u8 *)&data->block[1];
467 iface->manual_stop = 0;
468 iface->read_write = read_write;
469 iface->command = command;
470 iface->timeout_count = 10;
471 init_completion(&(iface->complete));
473 /* FIFO Initiation. Data in FIFO should be discarded before
474 * start a new operation.
476 write_FIFO_CTL(iface, 0x3);
478 write_FIFO_CTL(iface, 0);
481 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
483 /* Set Transmit device address */
484 write_MASTER_ADDR(iface, addr);
487 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
488 add_timer(&iface->timeout_timer);
490 switch (iface->cur_mode) {
491 case TWI_I2C_MODE_STANDARDSUB:
492 write_XMT_DATA8(iface, iface->command);
493 write_INT_MASK(iface, MCOMP | MERR |
494 ((iface->read_write == I2C_SMBUS_READ) ?
498 if (iface->writeNum + 1 <= 255)
499 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
501 write_MASTER_CTL(iface, 0xff << 6);
502 iface->manual_stop = 1;
505 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
506 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
508 case TWI_I2C_MODE_COMBINED:
509 write_XMT_DATA8(iface, iface->command);
510 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
513 if (iface->writeNum > 0)
514 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
516 write_MASTER_CTL(iface, 0x1 << 6);
518 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
519 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
522 write_MASTER_CTL(iface, 0);
523 if (size != I2C_SMBUS_QUICK) {
524 /* Don't access xmit data register when this is a
527 if (iface->read_write != I2C_SMBUS_READ) {
528 if (iface->writeNum > 0) {
529 write_XMT_DATA8(iface,
530 *(iface->transPtr++));
531 if (iface->writeNum <= 255)
532 write_MASTER_CTL(iface,
533 iface->writeNum << 6);
535 write_MASTER_CTL(iface,
537 iface->manual_stop = 1;
541 write_XMT_DATA8(iface, iface->command);
542 write_MASTER_CTL(iface, 1 << 6);
545 if (iface->readNum > 0 && iface->readNum <= 255)
546 write_MASTER_CTL(iface,
547 iface->readNum << 6);
548 else if (iface->readNum > 255) {
549 write_MASTER_CTL(iface, 0xff << 6);
550 iface->manual_stop = 1;
552 del_timer(&iface->timeout_timer);
557 write_INT_MASK(iface, MCOMP | MERR |
558 ((iface->read_write == I2C_SMBUS_READ) ?
563 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
564 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
565 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
570 wait_for_completion(&iface->complete);
572 rc = (iface->result >= 0) ? 0 : -1;
578 * Return what the adapter supports
580 static u32 bfin_twi_functionality(struct i2c_adapter *adap)
582 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
583 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
584 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
585 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
588 static struct i2c_algorithm bfin_twi_algorithm = {
589 .master_xfer = bfin_twi_master_xfer,
590 .smbus_xfer = bfin_twi_smbus_xfer,
591 .functionality = bfin_twi_functionality,
594 static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
596 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
598 iface->saved_clkdiv = read_CLKDIV(iface);
599 iface->saved_control = read_CONTROL(iface);
601 free_irq(iface->irq, iface);
604 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
609 static int i2c_bfin_twi_resume(struct platform_device *pdev)
611 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
613 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
614 IRQF_DISABLED, pdev->name, iface);
616 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
620 /* Resume TWI interface clock as specified */
621 write_CLKDIV(iface, iface->saved_clkdiv);
624 write_CONTROL(iface, iface->saved_control);
629 static int i2c_bfin_twi_probe(struct platform_device *pdev)
631 struct bfin_twi_iface *iface;
632 struct i2c_adapter *p_adap;
633 struct resource *res;
635 unsigned int clkhilow;
637 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
639 dev_err(&pdev->dev, "Cannot allocate memory\n");
641 goto out_error_nomem;
644 spin_lock_init(&(iface->lock));
646 /* Find and map our resources */
647 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
651 goto out_error_get_res;
654 iface->regs_base = ioremap(res->start, res->end - res->start + 1);
655 if (iface->regs_base == NULL) {
656 dev_err(&pdev->dev, "Cannot map IO\n");
658 goto out_error_ioremap;
661 iface->irq = platform_get_irq(pdev, 0);
662 if (iface->irq < 0) {
663 dev_err(&pdev->dev, "No IRQ specified\n");
665 goto out_error_no_irq;
668 init_timer(&(iface->timeout_timer));
669 iface->timeout_timer.function = bfin_twi_timeout;
670 iface->timeout_timer.data = (unsigned long)iface;
672 p_adap = &iface->adap;
673 p_adap->nr = pdev->id;
674 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
675 p_adap->algo = &bfin_twi_algorithm;
676 p_adap->algo_data = iface;
677 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
678 p_adap->dev.parent = &pdev->dev;
680 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
682 dev_err(&pdev->dev, "Can't setup pin mux!\n");
683 goto out_error_pin_mux;
686 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
687 IRQF_DISABLED, pdev->name, iface);
689 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
691 goto out_error_req_irq;
694 /* Set TWI internal clock as 10MHz */
695 write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
698 * We will not end up with a CLKDIV=0 because no one will specify
699 * 20kHz SCL or less in Kconfig now. (5 * 1024 / 20 = 0x100)
701 clkhilow = 5 * 1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ;
703 /* Set Twi interface clock as specified */
704 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
707 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
710 rc = i2c_add_numbered_adapter(p_adap);
712 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
713 goto out_error_add_adapter;
716 platform_set_drvdata(pdev, iface);
718 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
719 "regs_base@%p\n", iface->regs_base);
723 out_error_add_adapter:
724 free_irq(iface->irq, iface);
727 peripheral_free_list(pin_req[pdev->id]);
729 iounmap(iface->regs_base);
737 static int i2c_bfin_twi_remove(struct platform_device *pdev)
739 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
741 platform_set_drvdata(pdev, NULL);
743 i2c_del_adapter(&(iface->adap));
744 free_irq(iface->irq, iface);
745 peripheral_free_list(pin_req[pdev->id]);
746 iounmap(iface->regs_base);
752 static struct platform_driver i2c_bfin_twi_driver = {
753 .probe = i2c_bfin_twi_probe,
754 .remove = i2c_bfin_twi_remove,
755 .suspend = i2c_bfin_twi_suspend,
756 .resume = i2c_bfin_twi_resume,
758 .name = "i2c-bfin-twi",
759 .owner = THIS_MODULE,
763 static int __init i2c_bfin_twi_init(void)
765 return platform_driver_register(&i2c_bfin_twi_driver);
768 static void __exit i2c_bfin_twi_exit(void)
770 platform_driver_unregister(&i2c_bfin_twi_driver);
773 module_init(i2c_bfin_twi_init);
774 module_exit(i2c_bfin_twi_exit);
776 MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
777 MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
778 MODULE_LICENSE("GPL");
779 MODULE_ALIAS("platform:i2c-bfin-twi");