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>
17 #include <linux/timer.h>
18 #include <linux/spinlock.h>
19 #include <linux/completion.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
23 #include <asm/blackfin.h>
24 #include <asm/portmux.h>
27 #define POLL_TIMEOUT (2 * HZ)
30 #define TWI_I2C_MODE_STANDARD 1
31 #define TWI_I2C_MODE_STANDARDSUB 2
32 #define TWI_I2C_MODE_COMBINED 3
33 #define TWI_I2C_MODE_REPEAT 4
35 struct bfin_twi_iface {
47 struct timer_list timeout_timer;
48 struct i2c_adapter adap;
49 struct completion complete;
55 void __iomem *regs_base;
59 #define DEFINE_TWI_REG(reg, off) \
60 static inline u16 read_##reg(struct bfin_twi_iface *iface) \
61 { return bfin_read16(iface->regs_base + (off)); } \
62 static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
63 { bfin_write16(iface->regs_base + (off), v); }
65 DEFINE_TWI_REG(CLKDIV, 0x00)
66 DEFINE_TWI_REG(CONTROL, 0x04)
67 DEFINE_TWI_REG(SLAVE_CTL, 0x08)
68 DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
69 DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
70 DEFINE_TWI_REG(MASTER_CTL, 0x14)
71 DEFINE_TWI_REG(MASTER_STAT, 0x18)
72 DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
73 DEFINE_TWI_REG(INT_STAT, 0x20)
74 DEFINE_TWI_REG(INT_MASK, 0x24)
75 DEFINE_TWI_REG(FIFO_CTL, 0x28)
76 DEFINE_TWI_REG(FIFO_STAT, 0x2C)
77 DEFINE_TWI_REG(XMT_DATA8, 0x80)
78 DEFINE_TWI_REG(XMT_DATA16, 0x84)
79 DEFINE_TWI_REG(RCV_DATA8, 0x88)
80 DEFINE_TWI_REG(RCV_DATA16, 0x8C)
82 static const u16 pin_req[2][3] = {
83 {P_TWI0_SCL, P_TWI0_SDA, 0},
84 {P_TWI1_SCL, P_TWI1_SDA, 0},
87 static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface)
89 unsigned short twi_int_status = read_INT_STAT(iface);
90 unsigned short mast_stat = read_MASTER_STAT(iface);
92 if (twi_int_status & XMTSERV) {
93 /* Transmit next data */
94 if (iface->writeNum > 0) {
95 write_XMT_DATA8(iface, *(iface->transPtr++));
98 /* start receive immediately after complete sending in
101 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
102 write_MASTER_CTL(iface,
103 read_MASTER_CTL(iface) | MDIR | RSTART);
104 else if (iface->manual_stop)
105 write_MASTER_CTL(iface,
106 read_MASTER_CTL(iface) | STOP);
107 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
108 iface->cur_msg + 1 < iface->msg_num) {
109 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
110 write_MASTER_CTL(iface,
111 read_MASTER_CTL(iface) | RSTART | MDIR);
113 write_MASTER_CTL(iface,
114 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
118 write_INT_STAT(iface, XMTSERV);
121 if (twi_int_status & RCVSERV) {
122 if (iface->readNum > 0) {
123 /* Receive next data */
124 *(iface->transPtr) = read_RCV_DATA8(iface);
125 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
126 /* Change combine mode into sub mode after
129 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
130 /* Get read number from first byte in block
133 if (iface->readNum == 1 && iface->manual_stop)
134 iface->readNum = *iface->transPtr + 1;
138 } else if (iface->manual_stop) {
139 write_MASTER_CTL(iface,
140 read_MASTER_CTL(iface) | STOP);
142 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
143 iface->cur_msg + 1 < iface->msg_num) {
144 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
145 write_MASTER_CTL(iface,
146 read_MASTER_CTL(iface) | RSTART | MDIR);
148 write_MASTER_CTL(iface,
149 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
152 /* Clear interrupt source */
153 write_INT_STAT(iface, RCVSERV);
156 if (twi_int_status & MERR) {
157 write_INT_STAT(iface, MERR);
158 write_INT_MASK(iface, 0);
159 write_MASTER_STAT(iface, 0x3e);
160 write_MASTER_CTL(iface, 0);
162 iface->result = -EIO;
163 /* if both err and complete int stats are set, return proper
166 if (twi_int_status & MCOMP) {
167 write_INT_STAT(iface, MCOMP);
168 write_INT_MASK(iface, 0);
169 write_MASTER_CTL(iface, 0);
171 /* If it is a quick transfer, only address bug no data,
172 * not an err, return 1.
174 if (iface->writeNum == 0 && (mast_stat & BUFRDERR))
176 /* If address not acknowledged return -1,
179 else if (!(mast_stat & ANAK))
182 complete(&iface->complete);
185 if (twi_int_status & MCOMP) {
186 write_INT_STAT(iface, MCOMP);
188 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
189 if (iface->readNum == 0) {
190 /* set the read number to 1 and ask for manual
191 * stop in block combine mode
194 iface->manual_stop = 1;
195 write_MASTER_CTL(iface,
196 read_MASTER_CTL(iface) | (0xff << 6));
198 /* set the readd number in other
201 write_MASTER_CTL(iface,
202 (read_MASTER_CTL(iface) &
204 (iface->readNum << 6));
206 /* remove restart bit and enable master receive */
207 write_MASTER_CTL(iface,
208 read_MASTER_CTL(iface) & ~RSTART);
210 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
211 iface->cur_msg+1 < iface->msg_num) {
213 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
214 iface->writeNum = iface->readNum =
215 iface->pmsg[iface->cur_msg].len;
216 /* Set Transmit device address */
217 write_MASTER_ADDR(iface,
218 iface->pmsg[iface->cur_msg].addr);
219 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
220 iface->read_write = I2C_SMBUS_READ;
222 iface->read_write = I2C_SMBUS_WRITE;
223 /* Transmit first data */
224 if (iface->writeNum > 0) {
225 write_XMT_DATA8(iface,
226 *(iface->transPtr++));
232 if (iface->pmsg[iface->cur_msg].len <= 255)
233 write_MASTER_CTL(iface,
234 (read_MASTER_CTL(iface) &
236 (iface->pmsg[iface->cur_msg].len << 6));
238 write_MASTER_CTL(iface,
239 (read_MASTER_CTL(iface) |
241 iface->manual_stop = 1;
243 /* remove restart bit and enable master receive */
244 write_MASTER_CTL(iface,
245 read_MASTER_CTL(iface) & ~RSTART);
249 write_INT_MASK(iface, 0);
250 write_MASTER_CTL(iface, 0);
252 complete(&iface->complete);
257 /* Interrupt handler */
258 static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
260 struct bfin_twi_iface *iface = dev_id;
263 spin_lock_irqsave(&iface->lock, flags);
264 del_timer(&iface->timeout_timer);
265 bfin_twi_handle_interrupt(iface);
266 spin_unlock_irqrestore(&iface->lock, flags);
270 static void bfin_twi_timeout(unsigned long data)
272 struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data;
275 spin_lock_irqsave(&iface->lock, flags);
276 bfin_twi_handle_interrupt(iface);
277 if (iface->result == 0) {
278 iface->timeout_count--;
279 if (iface->timeout_count > 0) {
280 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
281 add_timer(&iface->timeout_timer);
284 complete(&iface->complete);
287 spin_unlock_irqrestore(&iface->lock, flags);
291 * Generic i2c master transfer entrypoint
293 static int bfin_twi_master_xfer(struct i2c_adapter *adap,
294 struct i2c_msg *msgs, int num)
296 struct bfin_twi_iface *iface = adap->algo_data;
297 struct i2c_msg *pmsg;
300 if (!(read_CONTROL(iface) & TWI_ENA))
303 while (read_MASTER_STAT(iface) & BUSBUSY)
307 iface->msg_num = num;
311 if (pmsg->flags & I2C_M_TEN) {
312 dev_err(&adap->dev, "10 bits addr not supported!\n");
316 iface->cur_mode = TWI_I2C_MODE_REPEAT;
317 iface->manual_stop = 0;
318 iface->transPtr = pmsg->buf;
319 iface->writeNum = iface->readNum = pmsg->len;
321 iface->timeout_count = 10;
322 init_completion(&(iface->complete));
323 /* Set Transmit device address */
324 write_MASTER_ADDR(iface, pmsg->addr);
326 /* FIFO Initiation. Data in FIFO should be
327 * discarded before start a new operation.
329 write_FIFO_CTL(iface, 0x3);
331 write_FIFO_CTL(iface, 0);
334 if (pmsg->flags & I2C_M_RD)
335 iface->read_write = I2C_SMBUS_READ;
337 iface->read_write = I2C_SMBUS_WRITE;
338 /* Transmit first data */
339 if (iface->writeNum > 0) {
340 write_XMT_DATA8(iface, *(iface->transPtr++));
347 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
349 /* Interrupt mask . Enable XMT, RCV interrupt */
350 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
353 if (pmsg->len <= 255)
354 write_MASTER_CTL(iface, pmsg->len << 6);
356 write_MASTER_CTL(iface, 0xff << 6);
357 iface->manual_stop = 1;
360 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
361 add_timer(&iface->timeout_timer);
364 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
365 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
366 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
369 wait_for_completion(&iface->complete);
380 * SMBus type transfer entrypoint
383 int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
384 unsigned short flags, char read_write,
385 u8 command, int size, union i2c_smbus_data *data)
387 struct bfin_twi_iface *iface = adap->algo_data;
390 if (!(read_CONTROL(iface) & TWI_ENA))
393 while (read_MASTER_STAT(iface) & BUSBUSY)
399 /* Prepare datas & select mode */
401 case I2C_SMBUS_QUICK:
402 iface->transPtr = NULL;
403 iface->cur_mode = TWI_I2C_MODE_STANDARD;
407 iface->transPtr = NULL;
409 if (read_write == I2C_SMBUS_READ)
413 iface->transPtr = &data->byte;
415 iface->cur_mode = TWI_I2C_MODE_STANDARD;
417 case I2C_SMBUS_BYTE_DATA:
418 if (read_write == I2C_SMBUS_READ) {
420 iface->cur_mode = TWI_I2C_MODE_COMBINED;
423 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
425 iface->transPtr = &data->byte;
427 case I2C_SMBUS_WORD_DATA:
428 if (read_write == I2C_SMBUS_READ) {
430 iface->cur_mode = TWI_I2C_MODE_COMBINED;
433 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
435 iface->transPtr = (u8 *)&data->word;
437 case I2C_SMBUS_PROC_CALL:
440 iface->cur_mode = TWI_I2C_MODE_COMBINED;
441 iface->transPtr = (u8 *)&data->word;
443 case I2C_SMBUS_BLOCK_DATA:
444 if (read_write == I2C_SMBUS_READ) {
446 iface->cur_mode = TWI_I2C_MODE_COMBINED;
448 iface->writeNum = data->block[0] + 1;
449 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
451 iface->transPtr = data->block;
453 case I2C_SMBUS_I2C_BLOCK_DATA:
454 if (read_write == I2C_SMBUS_READ) {
455 iface->readNum = data->block[0];
456 iface->cur_mode = TWI_I2C_MODE_COMBINED;
458 iface->writeNum = data->block[0];
459 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
461 iface->transPtr = (u8 *)&data->block[1];
468 iface->manual_stop = 0;
469 iface->read_write = read_write;
470 iface->command = command;
471 iface->timeout_count = 10;
472 init_completion(&(iface->complete));
474 /* FIFO Initiation. Data in FIFO should be discarded before
475 * start a new operation.
477 write_FIFO_CTL(iface, 0x3);
479 write_FIFO_CTL(iface, 0);
482 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
484 /* Set Transmit device address */
485 write_MASTER_ADDR(iface, addr);
488 iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
489 add_timer(&iface->timeout_timer);
491 switch (iface->cur_mode) {
492 case TWI_I2C_MODE_STANDARDSUB:
493 write_XMT_DATA8(iface, iface->command);
494 write_INT_MASK(iface, MCOMP | MERR |
495 ((iface->read_write == I2C_SMBUS_READ) ?
499 if (iface->writeNum + 1 <= 255)
500 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
502 write_MASTER_CTL(iface, 0xff << 6);
503 iface->manual_stop = 1;
506 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
507 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
509 case TWI_I2C_MODE_COMBINED:
510 write_XMT_DATA8(iface, iface->command);
511 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
514 if (iface->writeNum > 0)
515 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
517 write_MASTER_CTL(iface, 0x1 << 6);
519 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
520 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
523 write_MASTER_CTL(iface, 0);
524 if (size != I2C_SMBUS_QUICK) {
525 /* Don't access xmit data register when this is a
528 if (iface->read_write != I2C_SMBUS_READ) {
529 if (iface->writeNum > 0) {
530 write_XMT_DATA8(iface,
531 *(iface->transPtr++));
532 if (iface->writeNum <= 255)
533 write_MASTER_CTL(iface,
534 iface->writeNum << 6);
536 write_MASTER_CTL(iface,
538 iface->manual_stop = 1;
542 write_XMT_DATA8(iface, iface->command);
543 write_MASTER_CTL(iface, 1 << 6);
546 if (iface->readNum > 0 && iface->readNum <= 255)
547 write_MASTER_CTL(iface,
548 iface->readNum << 6);
549 else if (iface->readNum > 255) {
550 write_MASTER_CTL(iface, 0xff << 6);
551 iface->manual_stop = 1;
553 del_timer(&iface->timeout_timer);
558 write_INT_MASK(iface, MCOMP | MERR |
559 ((iface->read_write == I2C_SMBUS_READ) ?
564 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
565 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
566 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
571 wait_for_completion(&iface->complete);
573 rc = (iface->result >= 0) ? 0 : -1;
579 * Return what the adapter supports
581 static u32 bfin_twi_functionality(struct i2c_adapter *adap)
583 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
584 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
585 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
586 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
589 static struct i2c_algorithm bfin_twi_algorithm = {
590 .master_xfer = bfin_twi_master_xfer,
591 .smbus_xfer = bfin_twi_smbus_xfer,
592 .functionality = bfin_twi_functionality,
595 static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state)
597 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
599 iface->saved_clkdiv = read_CLKDIV(iface);
600 iface->saved_control = read_CONTROL(iface);
602 free_irq(iface->irq, iface);
605 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
610 static int i2c_bfin_twi_resume(struct platform_device *pdev)
612 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
614 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
615 IRQF_DISABLED, pdev->name, iface);
617 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
621 /* Resume TWI interface clock as specified */
622 write_CLKDIV(iface, iface->saved_clkdiv);
625 write_CONTROL(iface, iface->saved_control);
630 static int i2c_bfin_twi_probe(struct platform_device *pdev)
632 struct bfin_twi_iface *iface;
633 struct i2c_adapter *p_adap;
634 struct resource *res;
636 unsigned int clkhilow;
638 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
640 dev_err(&pdev->dev, "Cannot allocate memory\n");
642 goto out_error_nomem;
645 spin_lock_init(&(iface->lock));
647 /* Find and map our resources */
648 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
650 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
652 goto out_error_get_res;
655 iface->regs_base = ioremap(res->start, resource_size(res));
656 if (iface->regs_base == NULL) {
657 dev_err(&pdev->dev, "Cannot map IO\n");
659 goto out_error_ioremap;
662 iface->irq = platform_get_irq(pdev, 0);
663 if (iface->irq < 0) {
664 dev_err(&pdev->dev, "No IRQ specified\n");
666 goto out_error_no_irq;
669 init_timer(&(iface->timeout_timer));
670 iface->timeout_timer.function = bfin_twi_timeout;
671 iface->timeout_timer.data = (unsigned long)iface;
673 p_adap = &iface->adap;
674 p_adap->nr = pdev->id;
675 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
676 p_adap->algo = &bfin_twi_algorithm;
677 p_adap->algo_data = iface;
678 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
679 p_adap->dev.parent = &pdev->dev;
681 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
683 dev_err(&pdev->dev, "Can't setup pin mux!\n");
684 goto out_error_pin_mux;
687 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
688 IRQF_DISABLED, pdev->name, iface);
690 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
692 goto out_error_req_irq;
695 /* Set TWI internal clock as 10MHz */
696 write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F);
699 * We will not end up with a CLKDIV=0 because no one will specify
700 * 20kHz SCL or less in Kconfig now. (5 * 1024 / 20 = 0x100)
702 clkhilow = 5 * 1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ;
704 /* Set Twi interface clock as specified */
705 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
708 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
711 rc = i2c_add_numbered_adapter(p_adap);
713 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
714 goto out_error_add_adapter;
717 platform_set_drvdata(pdev, iface);
719 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
720 "regs_base@%p\n", iface->regs_base);
724 out_error_add_adapter:
725 free_irq(iface->irq, iface);
728 peripheral_free_list(pin_req[pdev->id]);
730 iounmap(iface->regs_base);
738 static int i2c_bfin_twi_remove(struct platform_device *pdev)
740 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
742 platform_set_drvdata(pdev, NULL);
744 i2c_del_adapter(&(iface->adap));
745 free_irq(iface->irq, iface);
746 peripheral_free_list(pin_req[pdev->id]);
747 iounmap(iface->regs_base);
753 static struct platform_driver i2c_bfin_twi_driver = {
754 .probe = i2c_bfin_twi_probe,
755 .remove = i2c_bfin_twi_remove,
756 .suspend = i2c_bfin_twi_suspend,
757 .resume = i2c_bfin_twi_resume,
759 .name = "i2c-bfin-twi",
760 .owner = THIS_MODULE,
764 static int __init i2c_bfin_twi_init(void)
766 return platform_driver_register(&i2c_bfin_twi_driver);
769 static void __exit i2c_bfin_twi_exit(void)
771 platform_driver_unregister(&i2c_bfin_twi_driver);
774 module_init(i2c_bfin_twi_init);
775 module_exit(i2c_bfin_twi_exit);
777 MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
778 MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
779 MODULE_LICENSE("GPL");
780 MODULE_ALIAS("platform:i2c-bfin-twi");