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>
15 #include <linux/slab.h>
18 #include <linux/timer.h>
19 #include <linux/spinlock.h>
20 #include <linux/completion.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
24 #include <asm/blackfin.h>
25 #include <asm/portmux.h>
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 {
45 struct i2c_adapter adap;
46 struct completion complete;
52 void __iomem *regs_base;
56 #define DEFINE_TWI_REG(reg, off) \
57 static inline u16 read_##reg(struct bfin_twi_iface *iface) \
58 { return bfin_read16(iface->regs_base + (off)); } \
59 static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
60 { bfin_write16(iface->regs_base + (off), v); }
62 DEFINE_TWI_REG(CLKDIV, 0x00)
63 DEFINE_TWI_REG(CONTROL, 0x04)
64 DEFINE_TWI_REG(SLAVE_CTL, 0x08)
65 DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
66 DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
67 DEFINE_TWI_REG(MASTER_CTL, 0x14)
68 DEFINE_TWI_REG(MASTER_STAT, 0x18)
69 DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
70 DEFINE_TWI_REG(INT_STAT, 0x20)
71 DEFINE_TWI_REG(INT_MASK, 0x24)
72 DEFINE_TWI_REG(FIFO_CTL, 0x28)
73 DEFINE_TWI_REG(FIFO_STAT, 0x2C)
74 DEFINE_TWI_REG(XMT_DATA8, 0x80)
75 DEFINE_TWI_REG(XMT_DATA16, 0x84)
76 DEFINE_TWI_REG(RCV_DATA8, 0x88)
77 DEFINE_TWI_REG(RCV_DATA16, 0x8C)
79 static const u16 pin_req[2][3] = {
80 {P_TWI0_SCL, P_TWI0_SDA, 0},
81 {P_TWI1_SCL, P_TWI1_SDA, 0},
84 static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
85 unsigned short twi_int_status)
87 unsigned short mast_stat = read_MASTER_STAT(iface);
89 if (twi_int_status & XMTSERV) {
90 /* Transmit next data */
91 if (iface->writeNum > 0) {
93 write_XMT_DATA8(iface, *(iface->transPtr++));
96 /* start receive immediately after complete sending in
99 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
100 write_MASTER_CTL(iface,
101 read_MASTER_CTL(iface) | MDIR | RSTART);
102 else if (iface->manual_stop)
103 write_MASTER_CTL(iface,
104 read_MASTER_CTL(iface) | STOP);
105 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
106 iface->cur_msg + 1 < iface->msg_num) {
107 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
108 write_MASTER_CTL(iface,
109 read_MASTER_CTL(iface) | RSTART | MDIR);
111 write_MASTER_CTL(iface,
112 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
115 if (twi_int_status & RCVSERV) {
116 if (iface->readNum > 0) {
117 /* Receive next data */
118 *(iface->transPtr) = read_RCV_DATA8(iface);
119 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
120 /* Change combine mode into sub mode after
123 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
124 /* Get read number from first byte in block
127 if (iface->readNum == 1 && iface->manual_stop)
128 iface->readNum = *iface->transPtr + 1;
132 } else if (iface->manual_stop) {
133 write_MASTER_CTL(iface,
134 read_MASTER_CTL(iface) | STOP);
135 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
136 iface->cur_msg + 1 < iface->msg_num) {
137 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
138 write_MASTER_CTL(iface,
139 read_MASTER_CTL(iface) | RSTART | MDIR);
141 write_MASTER_CTL(iface,
142 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
145 if (twi_int_status & MERR) {
146 write_INT_MASK(iface, 0);
147 write_MASTER_STAT(iface, 0x3e);
148 write_MASTER_CTL(iface, 0);
149 iface->result = -EIO;
151 if (mast_stat & LOSTARB)
152 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
153 if (mast_stat & ANAK)
154 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
155 if (mast_stat & DNAK)
156 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
157 if (mast_stat & BUFRDERR)
158 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
159 if (mast_stat & BUFWRERR)
160 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
162 /* if both err and complete int stats are set, return proper
165 if (twi_int_status & MCOMP) {
166 /* If it is a quick transfer, only address without data,
167 * not an err, return 1.
168 * If address is acknowledged return 1.
170 if ((iface->writeNum == 0 && (mast_stat & BUFRDERR))
171 || !(mast_stat & ANAK))
174 complete(&iface->complete);
177 if (twi_int_status & MCOMP) {
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
184 iface->manual_stop = 1;
185 write_MASTER_CTL(iface,
186 read_MASTER_CTL(iface) | (0xff << 6));
188 /* set the readd number in other
191 write_MASTER_CTL(iface,
192 (read_MASTER_CTL(iface) &
194 (iface->readNum << 6));
196 /* remove restart bit and enable master receive */
197 write_MASTER_CTL(iface,
198 read_MASTER_CTL(iface) & ~RSTART);
199 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
200 iface->cur_msg+1 < iface->msg_num) {
202 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
203 iface->writeNum = iface->readNum =
204 iface->pmsg[iface->cur_msg].len;
205 /* Set Transmit device address */
206 write_MASTER_ADDR(iface,
207 iface->pmsg[iface->cur_msg].addr);
208 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
209 iface->read_write = I2C_SMBUS_READ;
211 iface->read_write = I2C_SMBUS_WRITE;
212 /* Transmit first data */
213 if (iface->writeNum > 0) {
214 write_XMT_DATA8(iface,
215 *(iface->transPtr++));
220 if (iface->pmsg[iface->cur_msg].len <= 255)
221 write_MASTER_CTL(iface,
222 (read_MASTER_CTL(iface) &
224 (iface->pmsg[iface->cur_msg].len << 6));
226 write_MASTER_CTL(iface,
227 (read_MASTER_CTL(iface) |
229 iface->manual_stop = 1;
231 /* remove restart bit and enable master receive */
232 write_MASTER_CTL(iface,
233 read_MASTER_CTL(iface) & ~RSTART);
236 write_INT_MASK(iface, 0);
237 write_MASTER_CTL(iface, 0);
240 complete(&iface->complete);
243 /* Interrupt handler */
244 static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
246 struct bfin_twi_iface *iface = dev_id;
248 unsigned short twi_int_status;
250 spin_lock_irqsave(&iface->lock, flags);
252 twi_int_status = read_INT_STAT(iface);
255 /* Clear interrupt status */
256 write_INT_STAT(iface, twi_int_status);
257 bfin_twi_handle_interrupt(iface, twi_int_status);
260 spin_unlock_irqrestore(&iface->lock, flags);
265 * One i2c master transfer
267 static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
268 struct i2c_msg *msgs, int num)
270 struct bfin_twi_iface *iface = adap->algo_data;
271 struct i2c_msg *pmsg;
274 if (!(read_CONTROL(iface) & TWI_ENA))
277 while (read_MASTER_STAT(iface) & BUSBUSY)
281 iface->msg_num = num;
285 if (pmsg->flags & I2C_M_TEN) {
286 dev_err(&adap->dev, "10 bits addr not supported!\n");
290 iface->cur_mode = TWI_I2C_MODE_REPEAT;
291 iface->manual_stop = 0;
292 iface->transPtr = pmsg->buf;
293 iface->writeNum = iface->readNum = pmsg->len;
295 init_completion(&(iface->complete));
296 /* Set Transmit device address */
297 write_MASTER_ADDR(iface, pmsg->addr);
299 /* FIFO Initiation. Data in FIFO should be
300 * discarded before start a new operation.
302 write_FIFO_CTL(iface, 0x3);
304 write_FIFO_CTL(iface, 0);
307 if (pmsg->flags & I2C_M_RD)
308 iface->read_write = I2C_SMBUS_READ;
310 iface->read_write = I2C_SMBUS_WRITE;
311 /* Transmit first data */
312 if (iface->writeNum > 0) {
313 write_XMT_DATA8(iface, *(iface->transPtr++));
320 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
322 /* Interrupt mask . Enable XMT, RCV interrupt */
323 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
326 if (pmsg->len <= 255)
327 write_MASTER_CTL(iface, pmsg->len << 6);
329 write_MASTER_CTL(iface, 0xff << 6);
330 iface->manual_stop = 1;
334 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
335 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
336 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
339 while (!iface->result) {
340 if (!wait_for_completion_timeout(&iface->complete,
343 dev_err(&adap->dev, "master transfer timeout\n");
347 if (iface->result == 1)
348 rc = iface->cur_msg + 1;
356 * Generic i2c master transfer entrypoint
358 static int bfin_twi_master_xfer(struct i2c_adapter *adap,
359 struct i2c_msg *msgs, int num)
363 for (i = 0; i < adap->retries; i++) {
364 ret = bfin_twi_do_master_xfer(adap, msgs, num);
373 * One I2C SMBus transfer
375 int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
376 unsigned short flags, char read_write,
377 u8 command, int size, union i2c_smbus_data *data)
379 struct bfin_twi_iface *iface = adap->algo_data;
382 if (!(read_CONTROL(iface) & TWI_ENA))
385 while (read_MASTER_STAT(iface) & BUSBUSY)
391 /* Prepare datas & select mode */
393 case I2C_SMBUS_QUICK:
394 iface->transPtr = NULL;
395 iface->cur_mode = TWI_I2C_MODE_STANDARD;
399 iface->transPtr = NULL;
401 if (read_write == I2C_SMBUS_READ)
405 iface->transPtr = &data->byte;
407 iface->cur_mode = TWI_I2C_MODE_STANDARD;
409 case I2C_SMBUS_BYTE_DATA:
410 if (read_write == I2C_SMBUS_READ) {
412 iface->cur_mode = TWI_I2C_MODE_COMBINED;
415 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
417 iface->transPtr = &data->byte;
419 case I2C_SMBUS_WORD_DATA:
420 if (read_write == I2C_SMBUS_READ) {
422 iface->cur_mode = TWI_I2C_MODE_COMBINED;
425 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
427 iface->transPtr = (u8 *)&data->word;
429 case I2C_SMBUS_PROC_CALL:
432 iface->cur_mode = TWI_I2C_MODE_COMBINED;
433 iface->transPtr = (u8 *)&data->word;
435 case I2C_SMBUS_BLOCK_DATA:
436 if (read_write == I2C_SMBUS_READ) {
438 iface->cur_mode = TWI_I2C_MODE_COMBINED;
440 iface->writeNum = data->block[0] + 1;
441 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
443 iface->transPtr = data->block;
445 case I2C_SMBUS_I2C_BLOCK_DATA:
446 if (read_write == I2C_SMBUS_READ) {
447 iface->readNum = data->block[0];
448 iface->cur_mode = TWI_I2C_MODE_COMBINED;
450 iface->writeNum = data->block[0];
451 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
453 iface->transPtr = (u8 *)&data->block[1];
460 iface->manual_stop = 0;
461 iface->read_write = read_write;
462 iface->command = command;
463 init_completion(&(iface->complete));
465 /* FIFO Initiation. Data in FIFO should be discarded before
466 * start a new operation.
468 write_FIFO_CTL(iface, 0x3);
470 write_FIFO_CTL(iface, 0);
473 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
475 /* Set Transmit device address */
476 write_MASTER_ADDR(iface, addr);
479 switch (iface->cur_mode) {
480 case TWI_I2C_MODE_STANDARDSUB:
481 write_XMT_DATA8(iface, iface->command);
482 write_INT_MASK(iface, MCOMP | MERR |
483 ((iface->read_write == I2C_SMBUS_READ) ?
487 if (iface->writeNum + 1 <= 255)
488 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
490 write_MASTER_CTL(iface, 0xff << 6);
491 iface->manual_stop = 1;
494 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
495 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
497 case TWI_I2C_MODE_COMBINED:
498 write_XMT_DATA8(iface, iface->command);
499 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
502 if (iface->writeNum > 0)
503 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
505 write_MASTER_CTL(iface, 0x1 << 6);
507 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
508 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
511 write_MASTER_CTL(iface, 0);
512 if (size != I2C_SMBUS_QUICK) {
513 /* Don't access xmit data register when this is a
516 if (iface->read_write != I2C_SMBUS_READ) {
517 if (iface->writeNum > 0) {
518 write_XMT_DATA8(iface,
519 *(iface->transPtr++));
520 if (iface->writeNum <= 255)
521 write_MASTER_CTL(iface,
522 iface->writeNum << 6);
524 write_MASTER_CTL(iface,
526 iface->manual_stop = 1;
530 write_XMT_DATA8(iface, iface->command);
531 write_MASTER_CTL(iface, 1 << 6);
534 if (iface->readNum > 0 && iface->readNum <= 255)
535 write_MASTER_CTL(iface,
536 iface->readNum << 6);
537 else if (iface->readNum > 255) {
538 write_MASTER_CTL(iface, 0xff << 6);
539 iface->manual_stop = 1;
544 write_INT_MASK(iface, MCOMP | MERR |
545 ((iface->read_write == I2C_SMBUS_READ) ?
550 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
551 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
552 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
557 while (!iface->result) {
558 if (!wait_for_completion_timeout(&iface->complete,
561 dev_err(&adap->dev, "smbus transfer timeout\n");
565 rc = (iface->result >= 0) ? 0 : -1;
571 * Generic I2C SMBus transfer entrypoint
573 int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
574 unsigned short flags, char read_write,
575 u8 command, int size, union i2c_smbus_data *data)
579 for (i = 0; i < adap->retries; i++) {
580 ret = bfin_twi_do_smbus_xfer(adap, addr, flags,
581 read_write, command, size, data);
590 * Return what the adapter supports
592 static u32 bfin_twi_functionality(struct i2c_adapter *adap)
594 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
595 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
596 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
597 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
600 static struct i2c_algorithm bfin_twi_algorithm = {
601 .master_xfer = bfin_twi_master_xfer,
602 .smbus_xfer = bfin_twi_smbus_xfer,
603 .functionality = bfin_twi_functionality,
606 static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
608 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
610 iface->saved_clkdiv = read_CLKDIV(iface);
611 iface->saved_control = read_CONTROL(iface);
613 free_irq(iface->irq, iface);
616 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
621 static int i2c_bfin_twi_resume(struct platform_device *pdev)
623 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
625 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
626 IRQF_DISABLED, pdev->name, iface);
628 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
632 /* Resume TWI interface clock as specified */
633 write_CLKDIV(iface, iface->saved_clkdiv);
636 write_CONTROL(iface, iface->saved_control);
641 static int i2c_bfin_twi_probe(struct platform_device *pdev)
643 struct bfin_twi_iface *iface;
644 struct i2c_adapter *p_adap;
645 struct resource *res;
647 unsigned int clkhilow;
649 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
651 dev_err(&pdev->dev, "Cannot allocate memory\n");
653 goto out_error_nomem;
656 spin_lock_init(&(iface->lock));
658 /* Find and map our resources */
659 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
661 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
663 goto out_error_get_res;
666 iface->regs_base = ioremap(res->start, resource_size(res));
667 if (iface->regs_base == NULL) {
668 dev_err(&pdev->dev, "Cannot map IO\n");
670 goto out_error_ioremap;
673 iface->irq = platform_get_irq(pdev, 0);
674 if (iface->irq < 0) {
675 dev_err(&pdev->dev, "No IRQ specified\n");
677 goto out_error_no_irq;
680 p_adap = &iface->adap;
681 p_adap->nr = pdev->id;
682 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
683 p_adap->algo = &bfin_twi_algorithm;
684 p_adap->algo_data = iface;
685 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
686 p_adap->dev.parent = &pdev->dev;
687 p_adap->timeout = 5 * HZ;
690 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
692 dev_err(&pdev->dev, "Can't setup pin mux!\n");
693 goto out_error_pin_mux;
696 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
697 IRQF_DISABLED, pdev->name, iface);
699 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
701 goto out_error_req_irq;
704 /* Set TWI internal clock as 10MHz */
705 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
708 * We will not end up with a CLKDIV=0 because no one will specify
709 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
711 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
713 /* Set Twi interface clock as specified */
714 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
717 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
720 rc = i2c_add_numbered_adapter(p_adap);
722 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
723 goto out_error_add_adapter;
726 platform_set_drvdata(pdev, iface);
728 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
729 "regs_base@%p\n", iface->regs_base);
733 out_error_add_adapter:
734 free_irq(iface->irq, iface);
737 peripheral_free_list(pin_req[pdev->id]);
739 iounmap(iface->regs_base);
747 static int i2c_bfin_twi_remove(struct platform_device *pdev)
749 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
751 platform_set_drvdata(pdev, NULL);
753 i2c_del_adapter(&(iface->adap));
754 free_irq(iface->irq, iface);
755 peripheral_free_list(pin_req[pdev->id]);
756 iounmap(iface->regs_base);
762 static struct platform_driver i2c_bfin_twi_driver = {
763 .probe = i2c_bfin_twi_probe,
764 .remove = i2c_bfin_twi_remove,
765 .suspend = i2c_bfin_twi_suspend,
766 .resume = i2c_bfin_twi_resume,
768 .name = "i2c-bfin-twi",
769 .owner = THIS_MODULE,
773 static int __init i2c_bfin_twi_init(void)
775 return platform_driver_register(&i2c_bfin_twi_driver);
778 static void __exit i2c_bfin_twi_exit(void)
780 platform_driver_unregister(&i2c_bfin_twi_driver);
783 module_init(i2c_bfin_twi_init);
784 module_exit(i2c_bfin_twi_exit);
786 MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
787 MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
788 MODULE_LICENSE("GPL");
789 MODULE_ALIAS("platform:i2c-bfin-twi");